• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Saving state of a list

D

Deleted User

Guest
Hi,

Suppose I have a list view populated with data from a web service. I certainly don't want to take time hitting the WS if I could pull locally by saving the instance state or by some other means (meaning when the device is rotated or whatever causes the Activity to be re-created).

My question here is: What is the best way to do this?

I suppose I could work up a function to save each list item to SaveInstanceState by looping through each row and creating a dynamic name (ie: "listItem1", "listItem2", etc) and then read until listItemX doesn't exist (right?). Or would it be more appropriate to just write the values to a local db and pull from them (and would I highjack the onSaveInstanceState method to do this)?

Since I don't want to call the WS each time the Activity is created if the user likely won't get any new data, I was thinking of making a db table that stores the results from a previous WS. If that table is empty or the results are more than 5 minutes old, I will call the WS. I would also provide a refresh button for the user. How does this sound?

Thanks for your help
 
Looking into this deeper, I may have to do both? As pausing and resuming the Activity is not the same as stopping and starting. I imagine my db code would be in stop/start and I would also have save-instance code in the proper save instance handlers (invoked by pause/resume, I assume).

Is this correct? I'm trying to be most efficient here.
 
Upvote 0
You don't really need start or stop -- resume and pause are always called after / before start and stop, so just worry about those.

And I'm not sure, but can you just ptu your listview or the array adapter or whatever into your save instance bundle?
Thanks for your reply

That's what I'm thinking about doing. Is the method I described above the best way to do this? Or is there a simpler way?
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones