[英]Android onSaveInstanceState is always null
Consider the following method: 请考虑以下方法:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("repertoire",rep);
}
I'm using the menu NavigationDrawer
geneated by Android Studio. 我正在使用Android Studio生成的菜单
NavigationDrawer
。 When I go back to main activity, savedInstanceState
is always null
in my class: 当我回到主要活动时,类中的
savedInstanceState
始终为null
:
public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks : 公共类MainActivity扩展ActionBarActivity实现NavigationNavigatorerFragment.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));
}
I'm not 100% sure, but shoudln't you restore you activity in the onRestoreInstanceState()? 我不确定100%,但是您不应该在onRestoreInstanceState()中恢复活动吗? Again I'm not 100% sure but it might work.
同样,我不是100%肯定,但它可能有效。
Go ahead and create it! 继续创建它!
@Override
public void onSaveInstanceState(Bundle outState) {
if (outState == null) {
outState = new Bundle();
}
outState.putSerializable("repertoire", rep);
super.onSaveInstanceState(outState);
}
The super method doesn't do anything unless a non-null Bundle
is passed to it and its super method is just a stub! 除非将非null
Bundle
传递给super方法,并且super方法只是一个存根,否则super方法不会执行任何操作! The view hierarchy is saved separately. 视图层次结构是单独保存的。 You'll now receive the
Bundle
as a parameter in both onCreate(Bundle)
and onRestoreInstanceState(Bundle)
. 现在,您将在
onCreate(Bundle)
和onRestoreInstanceState(Bundle)
中将Bundle
作为参数接收。
My current code: In onCreate : 我当前的代码:在onCreate中:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( savedInstanceState != null ) {
System.out.println("back");
rep = (repertoire) savedInstanceState.getSerializable("repertoire");
}
and : 和:
@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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.