android onSaveInstanceState调用时机详细总结

首先给出onSaveInstanceState会调用的情况:

1、当用户按下HOME键时。
android onSaveInstanceState调用时机详细总结_第1张图片
2、长按HOME键,选择运行其他的程序时(即进入后台管理界面)。
android onSaveInstanceState调用时机详细总结_第2张图片

3、按下电源按键(关闭屏幕显示)时。
在这里插入图片描述

4、从activity A中启动一个新的activity时。
android onSaveInstanceState调用时机详细总结_第3张图片

5、屏幕方向切换时,例如从竖屏切换到横屏时。
android onSaveInstanceState调用时机详细总结_第4张图片
下面总结下:
1、看下官方解释

/**
     * Called to retrieve per-instance state from an activity before being killed
     * so that the state can be restored in {@link #onCreate} or
     * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
     * will be passed to both).
     *
     * 

This method is called before an activity may be killed so that when it * comes back some time in the future it can restore its state. For example, * if activity B is launched in front of activity A, and at some point activity * A is killed to reclaim resources, activity A will have a chance to save the * current state of its user interface via this method so that when the user * returns to activity A, the state of the user interface can be restored * via {@link #onCreate} or {@link #onRestoreInstanceState}. * *

Do not confuse this method with activity lifecycle callbacks such as * {@link #onPause}, which is always called when an activity is being placed * in the background or on its way to destruction, or {@link #onStop} which * is called before destruction. One example of when {@link #onPause} and * {@link #onStop} is called and not this method is when a user navigates back * from activity B to activity A: there is no need to call {@link #onSaveInstanceState} * on B because that particular instance will never be restored, so the * system avoids calling it. An example when {@link #onPause} is called and * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A: * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't * killed during the lifetime of B since the state of the user interface of * A will stay intact. * *

If called, this method will occur before {@link #onStop}. There are * no guarantees about whether it will occur before or after {@link #onPause}. */

从中可以得知:

  • 该方法是在Activity被杀之前调用以便之后恢复状态
  • 举例:如果activity B启用后位于activity A的之前,当activity A因为系统回收资源的问题要被杀掉,A通过onSaveInstanceState将有机会保存其用户界面状态,使得将来用户返回到activity A时能通过onCreate(Bundle)或者onRestoreInstanceState(Bundle)恢复界面的状态。
  • 要区分开onPause(Activity切到后台或者销毁时必定调用)和onStop(Activity销毁时必定调用)举例:会调用onPause()和onStop(),但不调用onSaveInstanceState的例子是当用户从Activity B返回到Activity A时:没有必要调用B的onSaveInstanceState(Bundle),因为B实例永远不会被恢复,因此系统会避免调用它。调用onPause()但不调用onSaveInstanceState的例子是当Activity B启动并处在activity A的前端:如果在B的整个生命周期里A的用户界面状态都没有被破坏的话,系统是不会调用Activity A的onSaveInstanceState(Bundle)的。
  • onSaveInstanceState的调用时机不能保证是在onPause之前还是之后,但是一定会在onStop()前被触发

2、总结来说就是onSaveInstanceState会在未经用户主动操作而导致Activity 销毁(亦可理解为Activity异常的销毁)的情况下系统会调用该方法以便保存状态在将来恢复 。此处的主动操作是指主动调用finish()方法销毁activity或者按下返回键退出

你可能感兴趣的:(Android随记)