android - How to persist big RecyclerView data on orientation changes? -


i'm making app content structure similar reddit. in mainactivity.java have recyclerview possibly have content. content loaded web, , don't want load again everytime when screen rotates.

so, have recyclerview setup this:

mainactivity.java

list<customitem> data;       // volley , gson magic load content web data list gsonrequest gsonrequest = new gsonrequest<customresponse>("http://example.json",     customresponse.class, null, new response.listener<customresponse>(){     @override     public void onresponse(customresponse response) {         for(customitem item : response.getitems()){             data.add(item);         }          madapter = new customadapter(data);         mrecycler.setadapter(madapter);     },     new response.errorlistener(){         @override         public void onerrorresponse(volleyerror error) {             log.d("e", error.getmessage());         } });   volleysingleton.getinstance().getrequestqueue().add(gsonrequest); 

customresponse.class

public class customresponse {     @serializedname("items")     private list<customitem> items;      public list<customitem> getitems(){          return items;     } 

customitem.java this, although have lot more variables in it

public class customitem {     @serializedname("id")  // awesome gson magic     public long mid;      @serializedname("title");     public string mtitle;      @serializename("another_item")     public anotheritem manotheritem; // custom object need } 

obviously data loaded every time screen orientation changes.

my question is: how persist data on recyclerview when configuration changes? what's best way?

i'm thinking of 2 options:

  1. making list<customitem> data parcelable, , saving in onsaveinstancestate() , restore data when needed
  2. using sqlite write contents database, , populate recyclerview there. remove content there when not needed.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -