考虑以下方法:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("repertoire",rep);
}
我使用Android Studio生成的菜单NavigationDrawer
。当我回到主要活动时,我的班级savedInstanceState
总是null
:
公共类MainActivity扩展ActionBarActivity实现NavigationDrawerFragment.NavigationDrawerCallbacks:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( savedInstanceState != null ) {
rep = (repertoire) savedInstanceState.getSerializable("repertoire");
}
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
ListView vue = (ListView) findViewById(R.id.listView);
if(rep==null){
rep = new repertoire();
}
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
答案 0 :(得分:-1)
我不是百分百肯定,但是你是否应该恢复onRestoreInstanceState()中的活动?我不是100%肯定,但可能会有效。
答案 1 :(得分:-1)
继续创建吧!
@Override
public void onSaveInstanceState(Bundle outState) {
if (outState == null) {
outState = new Bundle();
}
outState.putSerializable("repertoire", rep);
super.onSaveInstanceState(outState);
}
除非向它传递非null Bundle
并且它的super方法只是一个存根,否则super方法不会做任何事情!视图层次结构单独保存。您现在会在Bundle
和onCreate(Bundle)
中收到onRestoreInstanceState(Bundle)
作为参数。
答案 2 :(得分:-1)
我目前的代码: 在onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( savedInstanceState != null ) {
System.out.println("back");
rep = (repertoire) savedInstanceState.getSerializable("repertoire");
}
和:
@Override
public void onSaveInstanceState(Bundle outState) {
System.out.println("bibi");
if (outState == null) {
outState = new Bundle();
}
super.onSaveInstanceState(outState);
outState.putSerializable("repertoire", rep);
}
@Override
public void onRestoreInstanceState(Bundle bundle){
System.out.println("okm");
super.onRestoreInstanceState(bundle);
bundle.putSerializable("repertoire",rep);
}