基于Android8.1系统源码中的注释翻译
翻译如下:
在一个Activity被杀死之前被调用以取出Activity中每个对象实例的状态,这些状态可以在onCreate或者onRestoreInstanceState方法中被重新加载到实例(此方法产生的Bundle将传递到那两个方法)。
这个方法在一个Activity可能被杀掉之前(不是刚刚被杀之前)被调用,以便于之后某些时候Activity被重新打开时可以恢复它的(某些变量的)状态。比如,如果ActivityB被打开并显示在ActivityA之前(ActivityA被置于后台),在某些时候ActivityA由于系统资源回收被杀掉,AvtivityA将有机会使用此方法来保存用户界面的当前状态,以便当用户回到ActivityA时,用户界面的状态可以在Activity的onCreate或者onRestoreInstanceState中被恢复。
不要把这个方法和Activity的生命周期比如onPause和onStop混淆。比如,当从ActivityB退出返回到ActivityA时,onPause和onStop将被调用,但是onSaveInstanceState不会被调用,此时没有必要对ActivityB调用此方法,因为ActivityB将被销毁,并且不会被恢复。另外一个例子,当ActivityB被打开并显示在ActivityA之前时,系统可能会避免调用ActivityA的onSaveInstanceState方法,因为在ActivityB的生命周期内,ActivityA的ui状态可能一直保持完整。
大部分UI组件的实例的(状态保存)默认实现是通过对布局中有id属性的view调用view自身的onSaveInstanceState()方法,通过id保存当前有焦点的view(通过view的onRestoreInstanceState来恢复状态)。如果你想使用这个方法来保存某个view的状态,最好使用view自身的onSaveInstanceState来处理,否则每个(Activity中)的view实例都需要你写代码来维护。
如果此方法被调用,将在onStop之前发生,但是不能保证肯定是在onPause之后发生。
原文如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/** * 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). * * <p>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}. * * <p>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. * * <p>The default implementation takes care of most of the UI per-instance * state for you by calling {@link android.view.View#onSaveInstanceState()} on each * view in the hierarchy that has an id, and by saving the id of the currently * focused view (all of which is restored by the default implementation of * {@link #onRestoreInstanceState}). If you override this method to save additional * information not captured by each individual view, you will likely want to * call through to the default implementation, otherwise be prepared to save * all of the state of each view yourself. * * <p>If called, this method will occur before {@link #onStop}. There are * no guarantees about whether it will occur before or after {@link #onPause}. * * @param outState Bundle in which to place your saved state. * * @see #onCreate * @see #onRestoreInstanceState * @see #onPause */ |
Sorry, the comment form is closed at this time.