1

Hello Everyone, I had do a search on activity retain state.So I am getting to options/solutions for that:-

  1. Using savedInstanceState() and retainInstanceState() methods.
  2. Using Parent Child hierarchy declare in manifest and its working. (Example Url:-How can I return to a parent activity correctly? I had same problem mention in Note scetion of the answer.That case is match with my problem.

So,I want to retain the activity states just like Whats app(call/chats/contacts fragments).

  • But In My scenario,I am fetching the contacts from server.So how can I persist my data while switching between chat and my fragment activity?
  • So while timing to fetch new data of my contact list from server.I want to save ui of my listview/recyclerview with old data previously I had.

Also suggest which method is good from above two methods or need to implement in other way.

Community
  • 1
  • 1
Anil Ravsaheb Ghodake
  • 1,587
  • 2
  • 27
  • 45
  • Cache the list in SQLite? – OneCricketeer Jul 26 '16 at 13:42
  • With Activities in Android having Lifecycles, I think a good option would be to 'decouple' you're Activities from your Data/Model. Have you thought about using a Model/View/Presenter (MVP) structure? A small app I wrote using a MVP structure - code is quite heavily commented so should be easy to follow - https://github.com/TreeFrogApps/A3_WeatherService – Mark Jul 26 '16 at 13:55
  • @Mark Keen Have you any other example in which you are using MVP structure to keep searched weather data cached while I am switching between two activities. – Anil Ravsaheb Ghodake Jul 27 '16 at 05:20
  • @Anil The example uses a type of LRUCache that retains WeatherData, for a reasonable time until its considered 'stale'. If you are navigating to another activity, then pressing back will pop the backstack and return you to the previous Activity. The MVP structure I use keeps the associated Activities' Presenter in a retained headless fragment that uses setRetainInstance(true). With this in mind you can persist the Presenter, and any data/lists etc. that are required by the UI. So even if onCreate in the Activity is called it doesn't matter as you're retainedFragment holds the Presenter. – Mark Jul 27 '16 at 12:37

0 Answers0