onSaveInstanceState和onRestoreInstanceState触发的时机

先看图:

Android中的activity通过onSaveInstanceState()方法保存activity的状态在onCreate或者onRestoreInstanceState方法恢复状态,如果是通过onRestart方法前端运行的话,只会执行onSaveinstanceState方法。直接看代码:


[java]   view plain  copy
  1. package com.hebaijun.savestate;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6.   
  7. public class SaveStateActivity extends Activity {  
  8.     public static final String TAG = "SaveStateActivity";  
  9.   
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         if (savedInstanceState!=null) {  
  13.             Log.v(TAG, savedInstanceState.getString("data"));  
  14.         }  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         Log.v(TAG, "onCreate");  
  18.     }  
  19.       
  20.     @Override  
  21.     protected void onRestoreInstanceState(Bundle savedInstanceState) {  
  22.         Log.v(TAG, savedInstanceState.getString("data"));  
  23.         super.onRestoreInstanceState(savedInstanceState);  
  24.         Log.v(TAG, "onRestoreInstanceState");  
  25.     }  
  26.       
  27.     @Override  
  28.     protected void onSaveInstanceState(Bundle outState) {  
  29.         outState.putString("data""example");  
  30.         super.onSaveInstanceState(outState);  
  31.         Log.v(TAG, "onSaveInstanceState");  
  32.     }  
  33.       
  34.     @Override  
  35.     protected void onPause() {  
  36.         super.onPause();  
  37.         Log.v(TAG, "onPause");  
  38.     }  
  39.       
  40.     @Override  
  41.     protected void onResume() {  
  42.         super.onResume();  
  43.         Log.v(TAG, "onResume");  
  44.     }  
  45.       
  46.     @Override  
  47.     protected void onStart() {  
  48.         super.onStart();  
  49.         Log.v(TAG, "onStart");  
  50.     }  
  51.       
  52.     @Override  
  53.     protected void onRestart() {  
  54.         super.onRestart();  
  55.         Log.v(TAG, "onRestart");  
  56.     }  
  57. }  


main.xml

[html]   view plain  copy
  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/editText1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content" >  
  11.     EditText>  
  12.   
  13. LinearLayout>  
1.运行程序后我们按HOME键,然后我们重新进入程序,Log如图:
  • 我们可以看出onSaveInstanceState方法在onPause方法前执行。Android 3.0之后,onSaveInstanceStateonPause之后,onStop之前调用。
  • 程序恢复执行的时候调用了onRestart方法,并没有调用 onRestoreInstanceState方法
2.运行程序后按BACK键,Log如图:
  • 程序并没有调用onSaveInstanceState方法,因为按BACK键表明用户已经明确退出,所以不会执行保存状态的操作。就是说 onSaveInstanceState能不能被执行到是不确定的,要保存永久的数据话还是在onPause方法中用其他方式保存。
  • 如果在edittext中输入字符的话,重新执行也不会重新出现。

onRestoreInstanceState的执行时机

    private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {

                activity.mCalled = false;
                if (r.isPersistable()) {
                    mInstrumentation.callActivityOnCreate(activity,r.state, r.persistentState);
                } else {
                    mInstrumentation.callActivityOnCreate(activity,r.state);
                }
                r.activity = activity;
                r.stopped = true;
                if (!r.activity.mFinished) {
                    activity.performStart();
                    r.stopped = false;
                }
                if (!r.activity.mFinished) {
                    if (r.isPersistable()) {
                        if (r.state != null || r.persistentState != null) {
                            mInstrumentation.callActivityOnRestoreInstanceState(activity,r.state, r.persistentState);
                        }
                    } else if (r.state != null) {
                        mInstrumentation.callActivityOnRestoreInstanceState(activity,r.state);
                    }
                }
                if (!r.activity.mFinished) {
                    activity.mCalled = false;
                    if (r.isPersistable()) {
                        mInstrumentation.callActivityOnPostCreate(activity,r.state, r.persistentState);
                    } else {
                        mInstrumentation.callActivityOnPostCreate(activity,r.state);
                    }

   }

从代码的逻辑看出,OnRestoreInstanceStateperformStart之后,OnPostCreate之前调用,调用的条件是r.state != nullr.state是AMS传过来的,也就是说OnRestoreInstanceState的调用取决于AMS是否想恢复activity。

除此之外,r.state也会被传入onCreate函数,所以,对activity状态恢复的工作,可以在onCreate中进行,也可以在OnRestoreInstanceState中进行。

从Activity中onCreate函数OnRestoreInstanceState函数设计初衷看,onCreate函数用于恢复activity本身的状态信息,也包括其中的fragment信息;OnRestoreInstanceState函数用于恢复Activity中window和DecorView的状态。

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
            mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
                    ? mLastNonConfigurationInstances.fragments : null);

        }
        mFragments.dispatchCreate();
        getApplication().dispatchActivityCreated(this, savedInstanceState);
    }

    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        if (mWindow != null) {
            Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
            if (windowState != null) {
                mWindow.restoreHierarchyState(windowState);
            }
        }
    }

你可能感兴趣的:(Android开发)