incentivelabs.de

Blog about nerd stuff, programming and related tools

Network and Lifecycle

Published / by glethien / Leave a Comment

There are a lot of problems with the activity lifecycle in particular when the following situation occurs

  1. Your activity starts a network request to your backend
  2. The user rotates the device. The activity gets recreated with a different configuration
  3. Your network request is lost. The async task runs in the background but the results get posted to the nowhere

The solution for this is simple and there is no need for additional software. As soon as you open the activity, the network request gets started. As soon as the requests finishes the result will be posted into a cache. The activity now can retrieve the result. Using callbacks you can notify the activity when the request is done, but this is optional as the old activity don’t exist anymore when rotating the device. The new activity will first check the cache before starting a new request.

This way you have separated the network logic from the activity lifecycle and your logic lives longer and outside the lifecycle.

Leave a Reply

Your email address will not be published. Required fields are marked *