How to transfer RecyclerView big data to orientation changes?

I am creating an application with a content structure somewhat similar to Reddit. In mine MainActivity.java, I have a RecyclerView that can have really a lot of content. Content is downloaded from the Internet, and I do not want to download it again every time the screen rotates.

So, I have a RecyclerView setting 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 it has a lot more variables

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, data is loaded every time the screen orientation changes.

My question is: How to save data in my RecyclerView when changing the configuration? What is the best way?

I think only of two options:

  • List<CustomItem> data onSaveInstanceState()
  • Sqlite RecyclerView . , .
+4

All Articles