0

My application works this way:

ListView---->onListItemClick---->detailspage---->backpressed---->goes back to the list---->click the same item again---->same detailspage loads again.

The details page gets a lot of data from a server and populate its views.

So, it takes 2-4 secs every time an item is clicked in the listview. I have seen apps where they wont load any data if the same page is called 2nd time.

How can I do that?

Currently in my app its like this:

onCreate call AsyncTask to get data and populate the view

nothing in onResume, onPause, onStart, onStop, onDestroyed

user1537779
  • 2,311
  • 5
  • 28
  • 45
  • you cache your data so that you need not download them everytime. – Raghunandan May 24 '13 at 13:07
  • @Raghunandan Thanks for a quick reply. But since I am a newbie, I have no Idea how to do that. Any link/guide would be great. – user1537779 May 24 '13 at 13:09
  • it depends on what kind of data your downlaoding. can you explain in brief whether your downloading data is a image text.. – Raghunandan May 24 '13 at 13:10
  • Its a big `JSON data`, which also contains `image urls` meaning I will have to download(or cache) images too. – user1537779 May 24 '13 at 13:13
  • use can use lazy list of UIL for downloading images. parse the json data and store the urls. use a custom lsitview with a custom adapter along with view holder. use lazylist of UIL – Raghunandan May 24 '13 at 13:17
  • http://stackoverflow.com/questions/15621936/whats-lazylist. here's the link to for lazylist and UIL – Raghunandan May 24 '13 at 13:19
  • Yeah for images I am using LazyLoading. I have problem with the JSON Data I get, where will I store it and how will I know it belongs to a specific Instance of the Activity. – user1537779 May 24 '13 at 13:24
  • I din't understand your comment – Raghunandan May 24 '13 at 13:25
  • The only thing I will get is a Long String which I will parse to JSON and populate the Activity View. So, probably I have to cache that Long String? If so, how will I know that it belongs to a specific details page. Every list item takes me to the same details page, but the data is different. – user1537779 May 24 '13 at 13:30
  • when you click on the item you will know the position right. if you have a custom adapter you will have a getView() with position as the parameter. if you have a arraylist. you can use the position as the index. – Raghunandan May 24 '13 at 13:32

1 Answers1

0

You can keep the data for that domain object in a singleton and then when you enter your details page there are two ways to go.

If for instance you had a list of Person class.

public class Person {
    private String name;
    private Image img;
    ...
}

Then you could have a PersonCache that was a singleton caching the data for the last Person selected in your list:

public class PersonCache {
    private Person cachedPerson;
    private static PersonCache instance;

    private PersonCache(){
        ...
    }

    public PersonCache getInstance(){
        if(instance == null){
            instance = new PersonCache();
        }
        return instance;
    }

    public Person getCachedPerson(){
       return cachedPerson;
    }

    public void setCachedPerson(Person p){
        cachedPerson = p;
    }

}

So in onCreate when you finish fetching your JSON data you create a Person object and call setCachedPerson.

If you know that the data in the details page won't have been updated: In onCreate in details page you check if the object that has been selected is the same as the one cached in your singleton (if the objects have unique ids in your database you can look at those to check if it's the same).

If you don't know whether there's new data: You can use the If-Modified-Since technique when making your GET request in your AsyncTask.

Basically what you do is add a header parameter key: If-Modified-Since value: Sat, 29 Oct 1994 19:43:31 GMT

If the server has no new data it can respond with 304 and send no response body but if it has new data it will respond with 200 and send the data just like normal.

Implementing this would require some implementation on the server side as well. Here's some more info on the technique: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html (section 14.25)

Simon
  • 6,293
  • 2
  • 28
  • 34