ネットワーク呼び出しを介してデータをロードし、デバイスが回転してから onSaveInstanceState コールバックに格納されたデータをロードすると、アダプターに変更が通知されますが、回転後にアクティビティが空白になります。以下にコードスニペットを示します。
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()));
}
また、getParcelableArrayList("list"); の後のリスト サイズは 20 です。と呼ばれます。そして、マインドボグラーは、ifステートメントでアダプターが再度初期化されると、回転時にデータが表示されることです
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");
}
}