【发布时间】:2013-04-24 12:17:56
【问题描述】:
在按下 home 并重新打开应用程序后,我正在尝试恢复 Activity 的先前状态,因此我将需要的内容保存在 onSaveInstanceState 中:
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_ACTIVE_VIEW_ID, mActiveViewId);
Log.v(TAG, this.toString() + " onSaveInstanceState");
}
调用,但在 onCreate 内部,Bundle 始终是 null。
这是 OnCreate():
@Override
public void onCreate(Bundle inState) {
super.onCreate(inState);
setContentView(R.layout.main);
setMenuDrawer();
if (inState != null) {
int activeViewID = inState.getInt(STATE_ACTIVE_VIEW_ID);
View activeView = findViewById(activeViewID);
menuDrawer.setActiveView(activeView);
switchSelectedFragment(activeView);
Log.v(TAG, this.toString() + " onCreate " + inState.toString());
} else {
switchSelectedFragment(findViewById(R.id.homeView));
}
Log.v(TAG, this.toString() + " onCreate");
}
【问题讨论】:
-
onRestoreInstanceState(bundle) 和 onSaveInstanceState(bundle) 用于在活动方向更改期间保存状态。使用共享偏好会帮助您解决问题。
-
所以在主页按下后在 onSaveInstanceState 调用中创建的包不是以下 onCreate 读取的包?
-
您的期望是正确的,但是有一些事情会影响这种行为。您是否在清单中的
Activity上或启动时设置了任何标志?另外,发布您的menuDrawer.saveState()和onCreate()方法。 -
用 onCreate 修改帖子并删除 outState.putParcelable(STATE_MENUDRAWER, menuDrawer.saveState());因为它是有影响的
-
@Raghunandan 请你帮我解决这个问题stackoverflow.com/questions/20903545/…
标签: android bundle android-lifecycle