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");
}
}
Your
log
is before you retrieve the values insavedInstanceState
, 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:
Inside your adapter class add something along the lines of: