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:
- making
list<customitem> data
parcelable, , saving inonsaveinstancestate()
, restore data when needed - using sqlite write contents database, , populate recyclerview there. remove content there when not needed.
Comments
Post a Comment