• 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>
  • 相关阅读:
    数据挖掘实践(45):实战--啤酒产量时序分析(三)
    数据挖掘实践(44):实战--啤酒产量时序分析(二)
    数据挖掘实践(43):实战--啤酒产量时序分析(一)
    数据挖掘实践(42):算法基础(十四)时间序列分析(五)
    Java 之 数组 案例(不重复的数组&回形数)
    Java 之 数组中常见的异常
    Java 之 数组 Arrays 工具类
    Java 之 数组常用算法
    Java 之 多维数组(二维数组)
    Java 之 一维数组
  • 原文地址:https://www.cnblogs.com/yjpjy/p/12066455.html
Copyright © 2020-2023  润新知