我知道,这个问题之前在 stackoverflow 上被问过,但没有一个答案对我有用。
可能值得一提:
- 我将 ActionBarSherlock 与支持包一起使用。
onSaveInstanceState
当我按下主页按钮时调用方法。该方法onCreate
总是为Bundle savedInstanceState
.onRestoreInstanceState
根本不会调用方法。(我不介意是否onCreate
有效;))。- 另外(没关系)我
super.onSaveInstanceState(outState)
试着把onSaveInstanceState
. 也没有运气。
这是代码。我希望有人遇到这个问题并解决了它。
public class MainActivity extends SherlockFragmentActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
private static String STATE_TO_STORE = "state_to_store";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(LOG_TAG, "onCreate: savedInstanceState = " + (savedInstanceState == null ? "NULL" : "Not NULL"));
// ... more code...
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(LOG_TAG, "onRestoreInstanceState: savedInstanceState = " + (savedInstanceState == null ? "NULL" : "Not NULL"));
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_TO_STORE, 5); // store some int
Log.d(LOG_TAG, "onSaveInstanceState bundle: " + outState.toString());
}
// ... more code ...
}
日志清楚地表明onSaveInstanceState
正在调用并且 onCreate 获取savedInstanceState = NULL
。