SaveAndRestoreInstanceState

转屏场景

数据保存

android/app/servertransaction/ActivityRelaunchItem.java

    @Override
    public void execute(ClientTransactionHandler client, IBinder token,
            PendingTransactionActions pendingActions) {
        client.handleRelaunchActivity(mActivityClientRecord, pendingActions);
    }

ActivityThread.handleRelaunchActivity

@Override
public void handleRelaunchActivity(ActivityClientRecord tmp,
        PendingTransactionActions pendingActions) {
  
          handleRelaunchActivityInner(r, configChanges, tmp.pendingResults, tmp.pendingIntents,
                pendingActions, tmp.startsNotResumed, tmp.overrideConfig, "handleRelaunchActivity");
}
private void handleRelaunchActivityInner(ActivityClientRecord r, int configChanges,
        List<ResultInfo> pendingResults, List<ReferrerIntent> pendingIntents,
        PendingTransactionActions pendingActions, boolean startsNotResumed,
        Configuration overrideConfig, String reason) {
    // Preserve last used intent, it may be set from Activity#setIntent().
    final Intent customIntent = r.activity.mIntent;
    // Need to ensure state is saved.
    if (!r.paused) {
        performPauseActivity(r, false, reason, null /* pendingActions */);
    }
    if (!r.stopped) {
        callActivityOnStop(r, true /* saveState */, reason);
    }
   handleDestroyActivity(r.token, false, configChanges, true, reason);//getNonConfigInstance传递true,非转屏正常启动Activity时传递false
  
   r.activity = null;
  
   handleLaunchActivity(r, pendingActions, customIntent);
}

callActivityOnStop

private void callActivityOnStop(ActivityClientRecord r, boolean saveState, String reason) {
    // Before P onSaveInstanceState was called before onStop, starting with P it's
    // called after. Before Honeycomb state was always saved before onPause.
    final boolean shouldSaveState = saveState && !r.activity.mFinished && r.state == null
            && !r.isPreHoneycomb();
    final boolean isPreP = r.isPreP();
    if (shouldSaveState && isPreP) {
        callActivityOnSaveInstanceState(r);
    }
    r.activity.performStop(false /*preserveWindow*/, reason);

callActivityOnSaveInstanceState

private void callActivityOnSaveInstanceState(ActivityClientRecord r) {
    r.state = new Bundle();
    r.state.setAllowFds(false);

    mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);//r.state记录Bundle这个Parcelable数据
}
public void callActivityOnSaveInstanceState(Activity activity, Bundle outState) {
    activity.performSaveInstanceState(outState);
}
final void performSaveInstanceState(Bundle outState) {
    onSaveInstanceState(outState);
}

数据恢复

handleStartActivity

@Override
public void handleStartActivity(ActivityClientRecord r,
        PendingTransactionActions pendingActions) {
  
         activity.performStart("handleStartActivity");
         mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
}

callActivityOnRestoreInstanceState

public void callActivityOnRestoreInstanceState(Activity activity, Bundle savedInstanceState) {
    activity.performRestoreInstanceState(savedInstanceState);
}
final void performRestoreInstanceState(Bundle savedInstanceState) {
    onRestoreInstanceState(savedInstanceState);
}

其他

ActivityClientRecord

/** Activity client record, used for bookkeeping for the real {@link Activity} instance. */
public static final class ActivityClientRecord {
  public IBinder token;
  Activity activity;
  Window window;
  Activity.NonConfigurationInstances lastNonConfigurationInstances;
  Bundle state;
  ......
}