2

When i start my android app, it loads large amount of data from server in mainactivity and adapt it with listView and when listview is clicked , new activity will launch . When return back to mainactivity from current activity , my app again load data from server that was previously loaded? How can i use 1st time loaded data after returning back to same activity next time??

Roshan Sharma
  • 513
  • 2
  • 7
  • 20

3 Answers3

2

There are several possible approaches to your problem depending on what you want to do. First of all, in the activity where you download and process the data, you should override the onSaveInstanceState and onRestoreInstanceState methods so that your data is persisted and survives orientation changes (and not loose the work you did processing the data).

If your application simply opens a detail activity, from the main activity, then when you press back, your data will be available. You do not have to reload anything. If this is not the case, then there may be some problem with how you load the data in the activity's lifecycle (e.g. avoid loading and processing data in the onStart/onResume methods).

If you want to persist data after your application has died, you can either use http caching (e.g. with OkHttp) or use an Sqlite database, as others have pointed out. These approaches can also be combined for additional performance gains.

gmetal
  • 2,908
  • 2
  • 11
  • 16
1

You can cache the server response in shared preference in form of JSON and store to avoid making server calls each time. (However this is recommended only when data is small)

Example :

public <T> void putJsonIntoSharedPreferences(String key, T object) {
    SharedPreferences sharedPreferences = getSharedPreferences();
    SharedPreferences.Editor edit = sharedPreferences.edit();
    String json = convertToJson(object);
    edit.putString(key, json);
    edit.commit();
}


public <T> T getFromSharedPreferences(String key, Class<T> clazz) {
   SharedPreferences sharedPreferences = getSharedPreferences();
    final String fromSharedPreferences = sharedPreferences.getString(key, "");
    T object = null;
    if (!fromSharedPreferences.equals("")) {
        try {
            object = JsonConverter.fromJson(fromSharedPreferences, clazz);
        } catch (IOException e) {
            return null;
        }
    } else {
        try {
            return clazz.newInstance();
        } catch (Exception e) {

            return  null;
        }
    }
    return object;
}

private <T> T fromJson(String json, Class<T> clazz) throws IOException {
    ObjectMapper objectMapper = getObjectMapper();
    if(json!=null)
    return objectMapper.readValue(json,clazz);

    return null;
}

 private String convertToJson(Object object) {
    ObjectMapper objectMapper = new ObjectMapper();
    String string = null;
    try {
        string = objectMapper.writeValueAsString(object);
    } catch (JsonProcessingException e) {
        Log.d(ComponentConstants.JSON_PARSE_ERROR_TAG,e.getMessage(),e);
    }
    return string;
}
0

You can simply use an http client with caching like OkHttp. more complicated yet more appropriate in some cases is to manage an internal Sqlite Database.

As a Side note You can save all your data and load it when activity is reloaded but you should not. as no metter what way you choose, IO will be involved and its going to be a heavy operation if your data is big enough.

Its good practice to load exactly what the activity needs to show when its being loaded, and not the full data. You should use CursorLoader or CursorAdapter to load dynamically what your activity needs as user interacts with it...

Community
  • 1
  • 1
Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
  • sure i used sqlite database to store locally, my problem is everytime activity start it loads data from database?? Whether database is offline or online? My question is can i store that data in my app session?? – Roshan Sharma Jun 08 '16 at 09:06
  • can you please explain me about how post are easily handled in Facebook App?? – Roshan Sharma Jun 08 '16 at 09:12
  • @RoshanSharma thats a whole diffrent question – Ofek Ron Jun 08 '16 at 09:17
  • No it is similar . I want to load post as facebook loads? In facebook when return back to previous activity , previously fetched data are used☺☺ – Roshan Sharma Jun 08 '16 at 09:22
  • 1
    Because they only load what they need when they need it. loading few posts from an internal sqlite database may take a millisec or a few nanos. you can be sure they dont load all the posts in advance like you think of doing. – Ofek Ron Jun 08 '16 at 09:25