in order to refresh their data?
Here lies the answer. Activities should not own data. Activities present data and allow user to act upon it. Data itself should be in a separate entity that can be interacted with by activity instances, a Model.
Further, it should not be assumed that there is always an activity instance in back-stack. Those activities can be destroyed and later re-created as different objects by system when user navigates back. Data is always refreshed when the whole activity is being created.
Separate out data handling to specialized classes, that can be accessed by activities easily, and can provide data / event binding to activities on demand. A bound Service
is a good candidate.
As far as not loading data from web is concerned, you can setup a local cache of most recently accessed data (cache, because mobiles have strict storage limits, server and db not so). So, any change from user side is also committed to this cache along side propagating to the server. All this better be encapsulated by specialized data classes, rather than relying on a back-stack or special code in activity classes.
As a pattern you can construct Model
classes for data entities involved. Write a web API interface implementation to talk to the server. And then, place a cache layer before the API interface. The cache would retain outgoing changes and incoming updates to/from API layer and simply reflect data requests when server call is not needed.
Cache has to do 3 things mainly:
- Evict: as new data comes, drop the least important data, so cache remains of a fixed size. Most cache implementations (like this one) do this automatically.
- Invalidate: Some times, due to a user action, or external event on server side some data has to be refreshed.
- Expire: Data can be set with a time limit and will be auto evicted. This is helpful when enforcing a periodic refresh of data.
Now most cache implementations out there deal in raw bytes. I'd recommend using something like Realm, an object db and wrap it in cache like features. Hence a typical flow of requesting user tweets would be:
- Activity is displayed.
- Activity binds to data service, and expresses its interest in "tweets".
- Data service looks in
Tweets
table of cache db for last fetched list of tweets and immediately returns that.
- But, Data service also calls the server to give the tweets after time-stamp of latest tweet it has locally in db.
- Server returns latest set of tweets.
- Data service updates all the bound activities who expressed interest in tweets, with new incoming data. This data is also replicated locally in cache db. At this point, tweets table is also optimized by dropping old records.
- Activity unbinds from Data service, as the activity is going away, stopped, destroyed etc.
- If no bound activity is interested in "tweets", Data service stops loading more tweets.
Your data implementation can go a level further by maintaining socket connections to server and receive interesting changes in real-time. That is step 5 above will be called by server whenever there is new data incoming.
TLDR; Separate data management from Activities, and then you can evolve it independently of UI concerns.