四大组件之Activity

Activity,界面,源码中对它的翻译如下:

 * An activity is a single, focused thing that the user can do.  Almost all
 * activities interact with the user, so the Activity class takes care of
 * creating a window for you in which you can place your UI with
 * {@link #setContentView}.  While activities are often presented to the user
 * as full-screen windows, they can also be used in other ways: as floating
 * windows (via a theme with {@link android.R.attr#windowIsFloating} set)
 * or embedded inside of another activity (using {@link ActivityGroup}).

1. Activity的生命周期

Android生命周期图
上图为一个Activity的正常生命周期,但如果有多个activity之间跳转,两个交叉的生命周期有两个方法需要注意 ,即 ActivityA的 onPause 和 ActivityB的onCreate 才会走到。详情示例如下:
step1:启动ActivityA

03-30 10:46:25.125 20281-20281/com.android.sunny I/ActivityA: onCreate: 
03-30 10:46:25.126 20281-20281/com.android.sunny I/ActivityA: onStart: 
03-30 10:46:25.132 20281-20281/com.android.sunny I/ActivityA: onResume: 

step2:ActivityA 启动 ActivityB

03-30 10:48:08.354 20281-20281/com.android.sunny I/ActivityA: onPause: 
03-30 10:48:08.395 20281-20281/com.android.sunny I/ActivityB: onCreate: 
03-30 10:48:08.396 20281-20281/com.android.sunny I/ActivityB: onStart: 
03-30 10:48:08.396 20281-20281/com.android.sunny I/ActivityB: onResume: 
03-30 10:48:08.817 20281-20281/com.android.sunny I/ActivityA: onStop: 

step3:此时按下手机的返回按键

03-30 10:48:12.213 20281-20281/com.android.sunny I/ActivityB: onPause: 
03-30 10:48:12.227 20281-20281/com.android.sunny I/ActivityA: onActivityResult: 
03-30 10:48:12.228 20281-20281/com.android.sunny I/ActivityA: onRestart: 
03-30 10:48:12.229 20281-20281/com.android.sunny I/ActivityA: onStart: 
03-30 10:48:12.229 20281-20281/com.android.sunny I/ActivityA: onResume: 
03-30 10:48:12.636 20281-20281/com.android.sunny I/ActivityB: onStop: 
03-30 10:48:12.636 20281-20281/com.android.sunny I/ActivityB: onDestroy: 

step4:手机再次按下返回键

03-30 10:59:13.293 20281-20281/com.android.sunny I/ActivityA: onPause: 
03-30 10:59:13.554 20281-20281/com.android.sunny I/ActivityA: onStop: 
03-30 10:59:13.554 20281-20281/com.android.sunny I/ActivityA: onDestroy: 

PS: ActivityA 启动 ActivityB 的时候,先走完 ActivityA的 onPause 才继续走 ActivityB 的 onCreate 方法。源码中的说明如下:

* Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
 * {@link #onPause}, for your activity to start interacting with the user.
 * This is a good place to begin animations, open exclusive-access devices
 * (such as the camera), etc.
 *
 * <p>Keep in mind that onResume is not the best indicator that your activity
 * is visible to the user; a system window such as the keyguard may be in
 * front.  Use {@link #onWindowFocusChanged} to know for certain that your
 * activity is visible to the user (for example, to resume a game).
 *
 * <p><em>Derived classes must call through to the super class's
 * implementation of this method.  If they do not, an exception will be
 * thrown.</em></p>
  1. 异常情况下的生命周期

这里写图片描述
当系统配置发生改变后,Activity 会被销毁,其 onPause、onStop、onDestory 均会被调用,同时由于 Activity 是在异常情况下终止的,系统会调用 onSaveInstanceState 来保存当前 Activity 的状态。 这个方法的调用时机是在 onStop 之前,它和 onPause 没有既定的时序关系,它既可能在 onPause 之前调用,也可能在 onPause 之后调用。需要强调的是,onSaveInstanceState 仅在 Activity 被异常终止后才会调用。
当Activity 被异常终止后,再次重新创建时,系统会调用 onRestoreInstanceState 和 onCreate 方法来判断 Activity 是否被重建了,如果被重建了,我们就可以取出之前保存的数据并恢复,从时序上来说,onRestoreInstanceState 的调用时机在 onStart 之后。如下图:

03-30 15:40:07.546 25745-25745/com.android.sunny I/ActivityA: onCreate: 
03-30 15:40:07.547 25745-25745/com.android.sunny I/ActivityA: onStart: 
03-30 15:40:07.547 25745-25745/com.android.sunny I/ActivityA: onResume: 

03-30 15:40:13.610 25745-25745/com.android.sunny I/ActivityA: onPause: 
03-30 15:40:13.611 25745-25745/com.android.sunny I/ActivityA: onSaveInstanceState: 11
03-30 15:40:13.621 25745-25745/com.android.sunny I/ActivityA: onStop: 
03-30 15:40:13.621 25745-25745/com.android.sunny I/ActivityA: onDestroy: 
03-30 15:40:13.696 25745-25745/com.android.sunny I/ActivityA: onCreate: 
03-30 15:40:13.697 25745-25745/com.android.sunny I/ActivityA: onStart: 
03-30 15:40:13.697 25745-25745/com.android.sunny I/ActivityA: onRestoreInstanceState: savedInstanceState.getText = fffffff
03-30 15:40:13.698 25745-25745/com.android.sunny I/ActivityA: onResume: 

ActivityA 中的代码如下:
简单版

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    //此方法不会走到
    Log.i(TAG, "onSaveInstanceState: ");
    outState.putString("text", "dddd");
    super.onSaveInstanceState(outState, outPersistentState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    Log.i(TAG, "onSaveInstanceState: 11");
    outState.putString("text", "fffffff");
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    Log.i(TAG, "onRestoreInstanceState: savedInstanceState.getText = " + savedInstanceState.getString("text"));
    super.onRestoreInstanceState(savedInstanceState);
}

带有源码注释版:

/**
 * This is the same as {@link #onSaveInstanceState} but is called for activities
 * created with the attribute {@link android.R.attr#persistableMode} set to
 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
 * the first time that this activity is restarted following the next device reboot.
 *
 * @param outState Bundle in which to place your saved state.
 * @param outPersistentState State which will be saved across reboots.
 *
 * @see #onSaveInstanceState(Bundle)
 * @see #onCreate
 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
 * @see #onPause
 */
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    //此方法不会走到
    Log.i(TAG, "onSaveInstanceState: ");
    outState.putString("text", "dddd");
    super.onSaveInstanceState(outState, outPersistentState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    Log.i(TAG, "onSaveInstanceState: 11");
    outState.putString("text", "fffffff");
    super.onSaveInstanceState(outState);
}

/**
 * This method is called after {@link #onStart} when the activity is
 * being re-initialized from a previously saved state, given here in
 * <var>savedInstanceState</var>.  Most implementations will simply use {@link #onCreate}
 * to restore their state, but it is sometimes convenient to do it here
 * after all of the initialization has been done or to allow subclasses to
 * decide whether to use your default implementation.  The default
 * implementation of this method performs a restore of any view state that
 * had previously been frozen by {@link #onSaveInstanceState}.
 *
 * <p>This method is called between {@link #onStart} and
 * {@link #onPostCreate}.
 *
 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
 *
 * @see #onCreate
 * @see #onPostCreate
 * @see #onResume
 * @see #onSaveInstanceState
 */

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    Log.i(TAG, "onRestoreInstanceState: savedInstanceState.getText = " + savedInstanceState.getString("text"));
    super.onRestoreInstanceState(savedInstanceState);
}

猜你喜欢

转载自blog.csdn.net/sindyue/article/details/79756839
今日推荐