Alm*_*mer 5 android activity-lifecycle android-lifecycle
我知道,这个问题之前在stackoverflow上被问到,但是没有答案对我有用.
可能值得一提的是:
onSaveInstanceState我按下主页按钮时调用方法IS.该方法onCreate总是为NULL提供NULL 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 ...
}
Run Code Online (Sandbox Code Playgroud)
记录明确表示onSaveInstanceState正在调用和onCreate得到savedInstanceState = NULL.
当 Activity 由于缺乏资源而被系统终止并在您返回时重新启动时,将触发 onRestoreInstanceState(或 onCreate 中保存的包)。如果不经过 onRestoreInstanceState,Activity 可能不会被终止(只是停止)并重新启动。换句话说,onSaveInstanceState会一直被调用,但是onRestoreInstanceState如果被系统杀死并恢复,则会被调用。不只是停止和重新启动,不是暂停和恢复,也不是由新意图启动。
在这里检查我的解释。我确信它涵盖了你的问题。
| 归档时间: |
|
| 查看次数: |
19322 次 |
| 最近记录: |