AppCompatActivity执行流程

555 阅读1分钟

AppCompatActivity 和 Activity的基本源码

  1. AppCompatActivity 的基本类图

  1. AppCompatActivity onCreate加载时序图

  2. 核心类 --- AppCompatActivity

@Override protected void onCreate(@Nullable Bundle savedInstanceState) { final AppCompatDelegate delegate = getDelegate(); delegate.installViewFactory(); delegate.onCreate(savedInstanceState); super.onCreate(savedInstanceState); }

  1. 核心类 --- AppCompatDelegate

核心方法:installViewFactory() public void installViewFactory() { LayoutInflater layoutInflater = LayoutInflater.from(mContext); if (layoutInflater.getFactory() == null) { LayoutInflaterCompat.setFactory2(layoutInflater, this); } else { if (!(layoutInflater.getFactory2() instanceof AppCompatDelegateImpl)) { Log.i(TAG, "The Activity's LayoutInflater already has a Factory installed" + " so we can not install AppCompat's"); } } }

核心方法:onCreate() public void onCreate(Bundle savedInstanceState) { // attachBaseContext will only be called from an Activity, so make sure we switch this for // Dialogs, etc mBaseContextAttached = true; ​ // Our implicit call to applyDayNight() should not recreate until after the Activity is // created applyDayNight(false); ​ // We lazily fetch the Window for Activities, to allow DayNight to apply in // attachBaseContext ensureWindow(); ​ if (mHost instanceof Activity) { String parentActivityName = null; try { parentActivityName = NavUtils.getParentActivityName((Activity) mHost); } catch (IllegalArgumentException iae) { // Ignore in this case } if (parentActivityName != null) { // Peek at the Action Bar and update it if it already exists ActionBar ab = peekSupportActionBar(); if (ab == null) { mEnableDefaultActionBarUp = true; } else { ab.setDefaultDisplayHomeAsUpEnabled(true); } } } ​ mCreated = true; }

  1. 核心类 --- AppCompatDelegateImpl 构造函数

private AppCompatDelegateImpl(Context context, Window window, AppCompatCallback callback, Object host) { mContext = context; mAppCompatCallback = callback; mHost = host; ​ if (mLocalNightMode == MODE_NIGHT_UNSPECIFIED && mHost instanceof Dialog) { final AppCompatActivity activity = tryUnwrapContext(); if (activity != null) { // This code path is used to detect when this Delegate is a child Delegate from // an Activity, primarily for Dialogs. Dialogs use the Activity as it's Context, // so we want to make sure that the this 'child' delegate does not interfere // with the Activity config. The simplest way to do that is to match the // outer Activity's local night mode mLocalNightMode = activity.getDelegate().getLocalNightMode(); } } if (mLocalNightMode == MODE_NIGHT_UNSPECIFIED) { // Try and read the current night mode from our static store final Integer value = sLocalNightModes.get(mHost.getClass()); if (value != null) { mLocalNightMode = value; // Finally remove the value sLocalNightModes.remove(mHost.getClass()); } } ​ if (window != null) { attachToWindow(window); } ​ // Preload appcompat-specific handling of drawables that should be handled in a special // way (for tinting etc). After the following line completes, calls from AppCompatResources // to ResourceManagerInternal (in appcompat-resources) will handle those internal drawable // paths correctly without having to go through AppCompatDrawableManager APIs. AppCompatDrawableManager.preload(); } AppCompatDrawableManager中的preload()方法去加载资源。