After rotating device data is not getting loaded which was stored in onSaveInstanceState callback

80 views Asked by At

Loading the data through a network call and when the device rotates then the data stored in onSaveInstanceState callback,the adapter is notified of the change but the activity becomes blank after rotation. A code snippet given below:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   // Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
   // setSupportActionBar(toolbar);

    movies=new ArrayList<Movie_model>();

    madapter=new Movie_adapter(this,new Movie_adapter.OnItemClickListener(){
        @Override
        public void onItemClick(Movie_model movie_item) {
            Toast.makeText(getApplicationContext(), "Item Clicked", Toast.LENGTH_LONG).show();
            Intent intent=new Intent(getApplicationContext(),MovieDetail.class);
            intent.putExtra("Movie_item", movie_item);
            intent.putExtra("type", "normal");
            startActivity(intent);
        };
    },movies);
    mAdapterFav = new CustomCursorAdapter(this,new CustomCursorAdapter.OnItemClickListener(){
        @Override
        public void onItemClick(Movie_model movie_item) {
            Toast.makeText(getApplicationContext(), "Item Clicked", Toast.LENGTH_LONG).show();
            Intent intent=new Intent(getApplicationContext(),MovieDetail.class);
            intent.putExtra("Movie_item", movie_item);
            intent.putExtra("type", "fav");
            startActivity(intent);
        };});
    recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(),3);
    recyclerview.setLayoutManager(mLayoutManager);
    recyclerview.setItemAnimator(new DefaultItemAnimator());
    recyclerview.setAdapter(madapter);
  if(savedInstanceState!=null)
    {
       // movies.clear();
       Log.i("tag", String.valueOf(movies.size()));
        movies=savedInstanceState.getParcelableArrayList("list");
        madapter.notifyDataSetChanged();
        //recyclerview.setAdapter(madapter);
       // Log.i("tag", String.valueOf(movies.size()));
    }
    else
   {
        mnetworking= new Networking();
        mnetworking.execute(discover_movies);
       Log.i("tag","in");
    }

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelableArrayList("list",movies);
    Log.i("tag", String.valueOf(movies.size()));
}

Also note the list size is 20 after getParcelableArrayList("list"); is called. And the mind boggler is that when adapter is again initialized in the if statement ,data is shown upon rotataion

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   // Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
   // setSupportActionBar(toolbar);

    movies=new ArrayList<Movie_model>();

    madapter=new Movie_adapter(this,new Movie_adapter.OnItemClickListener(){
        @Override
        public void onItemClick(Movie_model movie_item) {
            Toast.makeText(getApplicationContext(), "Item Clicked", Toast.LENGTH_LONG).show();
            Intent intent=new Intent(getApplicationContext(),MovieDetail.class);
            intent.putExtra("Movie_item", movie_item);
            intent.putExtra("type", "normal");
            startActivity(intent);
        };
    },movies);
    mAdapterFav = new CustomCursorAdapter(this,new CustomCursorAdapter.OnItemClickListener(){
        @Override
        public void onItemClick(Movie_model movie_item) {
            Toast.makeText(getApplicationContext(), "Item Clicked", Toast.LENGTH_LONG).show();
            Intent intent=new Intent(getApplicationContext(),MovieDetail.class);
            intent.putExtra("Movie_item", movie_item);
            intent.putExtra("type", "fav");
            startActivity(intent);
        };});
    recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(),3);
    recyclerview.setLayoutManager(mLayoutManager);
    recyclerview.setItemAnimator(new DefaultItemAnimator());
    recyclerview.setAdapter(madapter);
  if(savedInstanceState!=null)
    {
       // movies.clear();
       Log.i("tag", String.valueOf(movies.size()));
        movies=savedInstanceState.getParcelableArrayList("list");
    // madapter.notifyDataSetChanged();
        madapter=new Movie_adapter(this,new Movie_adapter.OnItemClickListener(){
            @Override
            public void onItemClick(Movie_model movie_item) {
                Toast.makeText(getApplicationContext(), "Item Clicked", Toast.LENGTH_LONG).show();
                Intent intent=new Intent(getApplicationContext(),MovieDetail.class);
                intent.putExtra("Movie_item", movie_item);
                intent.putExtra("type", "normal");
                startActivity(intent);
            };
        },movies);
        recyclerview.setAdapter(madapter);
       Log.i("tag", String.valueOf(movies.size()));
    }
    else
   {
        mnetworking= new Networking();
        mnetworking.execute(discover_movies);
       Log.i("tag","in");
    }

}
1

There are 1 answers

4
Sammy T On

Your log is before you retrieve the values in savedInstanceState, you wouldn't be logging the retrieved values then.

It looks like you might have to add an addAll method to your adapter class(if you don't have one already), then add the movie data in that way:

if(savedInstanceState != null){
    movies = savedInstanceState.getParcelableArrayList("list");
    madapter.addAll(movies); // Make sure you add an addAll() in your adapter's class first
    madapter.notifyDataSetChanged();
}
// The rest of your code

Inside your adapter class add something along the lines of:

public void addAll(ArrayList al){
    for(int i=0; i < al.size; i++){
        // add to your adapter's data set
        // yourDataSet.add(al.get(i));
    }
}