Saving state of a list


Last Updated:

  1. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    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
     

    Advertisement
  2. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    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.
     
  3. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    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?
     
  4. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    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?
     

Share This Page

Loading...