onCreate不加载布局

Posted on 2019-12-19 10:48  TongMeng  阅读(390)  评论(0编辑  收藏  举报

如果Activity重写的是 onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) 而不是 onCreate(@Nullable Bundle savedInstanceState)

那么Activity不会加载布局

因为 onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) (API 21) 实际上也是调用的onCreate(@Nullable Bundle savedInstanceState)

源码:

    public void onCreate(@Nullable Bundle savedInstanceState,
            @Nullable PersistableBundle persistentState) {
        onCreate(savedInstanceState);
    }

 

而了解activity启动流程的就知道,真正启动Activity的实现都在ActivityThread,前面的调用过程略过

ActivityThread的方法performLaunchActivity中调用了Instrumentation类中的方法callActivityOnCreate方法,继而调用了TargetActivity中的onCreate方法。
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
......
Activity activity = null;
activity = mInstrumentation.newActivity( cl, component.getClassName(), r.intent);               
......
if (r.isPersistable()) {
     mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
     mInstrumentation.callActivityOnCreate(activity, r.state);
}
......
}

Instrumentation类中的方法callActivityOnCreate方法源码如下:

public void callActivityOnCreate(Activity activity, Bundle icicle) {
        prePerformCreate(activity);
        activity.performCreate(icicle);
        postPerformCreate(activity);
    }

  

调用的Activity.performCreate如下,当persistentState不为空才会触发 onCreate(icicle, persistentState);

 /** @hide */
    @VisibleForTesting
    public final ActivityThread getActivityThread() {
        return mMainThread;
    }

    final void performCreate(Bundle icicle) {
        performCreate(icicle, null);
    }

    final void performCreate(Bundle icicle, PersistableBundle persistentState) {
        mCanEnterPictureInPicture = true;
        restoreHasCurrentPermissionRequest(icicle);
        if (persistentState != null) {
            onCreate(icicle, persistentState);
        } else {
            onCreate(icicle);
        }
        writeEventLog(LOG_AM_ON_CREATE_CALLED, "performCreate");
        mActivityTransitionState.readState(icicle);

        mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
                com.android.internal.R.styleable.Window_windowNoDisplay, false);
        mFragments.dispatchActivityCreated();
        mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
    }

ps:persistentState 注解如下,大意是,如果Activity之前被关闭或者断电,可以通过onSaveInstanceState保存数据到PersistableBundle

,当Activity再次被重新创建的时候的时候通过onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) 恢复现场。

public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
onSaveInstanceState(outState);
}


if the activity is being re-initialized after
* previously being shut down or powered off then this Bundle contains the data it most
* recently supplied to outPersistentState in {@link #onSaveInstanceState}.
* <b><i>Note: Otherwise it is null.</i></b>