4

I'm making an app with content structure somewhat similar to Reddit. In my MainActivity.java I have a RecyclerView that could possibly have really much content. The content is loaded from the web, and I don't want to load it again everytime when the screen rotates.

So, I have a RecyclerView setup something like this:

MainActivity.java

List<CustomItem> data;


    // Volley and GSON magic to load content from the web into 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 is something like this, although I have a lot more variables in it

public class CustomItem {
    @SerializedName("id")  // that awesome GSON magic
    public long mID;

    @SerializedName("title");
    public String mTitle;

    @SerializeName("another_item")
    public AnotherItem mAnotherItem; // A custom object that I need
}

Obviously the data is loaded every time the screen orientation changes.

My question is: How do I persist the data on my RecyclerView when the configuration changes? What's the best way?

I'm thinking of only two options:

  1. making List<CustomItem> data Parcelable, and saving it in onSaveInstanceState() and restore the data when needed
  2. Using Sqlite to write the contents to a database, and populate my RecyclerView from there. Remove content from there when not needed.
Iiro Krankka
  • 4,959
  • 5
  • 38
  • 42
  • 2
    Use a Fragment without UI. Have you request in fragment. Retain the fragment and have appropriate callbacks to the activity. You attach and detach the fragment as the screen is destroyed and recreated. There are other options like using RxJava and cache the data and you subscribe and listen to the events on the main thread. You could use a sqlite also. COnsider using it if the content does not change oftern. – Raghunandan Aug 25 '15 at 08:50
  • Thanks! Can you maybe give me a couple of key pointers how to do the job with a Fragment without UI? A quick Google search was no luck for me. – Iiro Krankka Aug 25 '15 at 11:53
  • its there in the android developer site itself – Raghunandan Aug 25 '15 at 11:54
  • Seems like I still have to use Parcelable with Fragments for preserving a custom List? What are the advantages of using Fragments to save my data instead of using the standard onsaveinstancestate()/onrestoreinstancestate() way? It seems like using a Fragment would be just complicating things. – Iiro Krankka Aug 25 '15 at 21:45

0 Answers0