• onCreate不加载布局


    如果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>
  • 相关阅读:
    【2019/3/23】周进度报告
    第十周总结
    程序员修炼之道-从小工到专家阅读笔记01
    第九周总结
    用户模板和用户场景
    一维数组最大子数组续
    程序员的自我修养阅读笔记03
    第八周总结
    NABCD项目分析
    第七周总结
  • 原文地址:https://www.cnblogs.com/yjpjy/p/12066455.html
Copyright © 2020-2023  润新知