我很抱歉,如果这是一个愚蠢的问题,我只是在生产中有一个奇怪的行为可以解释,如果以某种方式onSaveInstanceState
被调用但onCreate
没有(或至少没有完成执行)。
以下是代码:
private LaunchedFrom launchedFrom = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some members initialization
if (savedInstanceState == null) {
Intent intent = getIntent();
launchedFrom = handleIntentAction(intent);
} else {
// Restoring saved state.
launchedFrom = LaunchedFrom.fromEvent(savedInstanceState.getString(BundleParams.KEY_LAUNCHED_FROM));
}
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(BundleParams.KEY_LAUNCHED_FROM, launchedFrom.getEvent());
}
private LaunchedFrom handleIntentAction(Intent intent) {
LaunchedFrom launchedFrom = LaunchedFrom.MAIN;
if (intent != null) {
// some logic to override launchedFrom
}
return launchedFrom;
}
我有时(很少)在onSaveInstanceState
中获得一个空的launchFrom ...
谢谢你的帮助......
答案 0 :(得分:-2)
void onRestoreInstanceState(Bundle savedInstanceState)
在onStart()和onPostCreate(Bundle)之间调用此方法。
void onSaveInstanceState(Bundle outState)
如果调用,此方法将在onStop()之前发生。没有 保证是否会在onPause()之前或之后发生。
我希望这会有所帮助。虽然没有定义何时可以调用onSaveInstanceState()。