整体时序
// DisplayContent.java
boolean updateDisplayOverrideConfigurationLocked(Configuration values,ActivityRecord starting, boolean deferResume,ActivityTaskManagerService.UpdateConfigurationResult result) {int changes = 0;boolean kept = true;mAtmService.deferWindowLayout();try {if (values != null) {if (mDisplayId == DEFAULT_DISPLAY) {// 先触发进程相关的Configuration变化changes = mAtmService.updateGlobalConfigurationLocked(values,false /* initLocale */, false /* persistent */,UserHandle.USER_NULL /* userId */);} else {changes = performDisplayOverrideConfigUpdate(values);}}if (!deferResume) {// 再触发Activity相关的Configuration的变化kept = mAtmService.ensureConfigAndVisibilityAfterUpdate(starting, changes);}} finally {mAtmService.continueWindowLayout();}.....
}
进程级别ConfigurationChangeItem
主要是更新app进程的Resource中的Configuration并触发Application/Service/Provider等的onConfigurationChanged回调。
System server
// ATMS.java
int updateGlobalConfigurationLocked(@NonNull Configuration values, boolean initLocale,boolean persistent, int userId) {......Slog.i(TAG, "Config changes=" + Integer.toHexString(changes) + " " + mTempConfig);......SparseArray<WindowProcessController> pidMap = mProcessMap.getPidMap();for (int i = pidMap.size() - 1; i >= 0; i--) {final int pid = pidMap.keyAt(i);final WindowProcessController app = pidMap.get(pid);ProtoLog.v(WM_DEBUG_CONFIGURATION, "Update process config of %s to new "+ "config %s", app.mName, mTempConfig);app.onConfigurationChanged(mTempConfig);}......
}
// WindowProcessController.java
private void scheduleConfigurationChange(IApplicationThread thread, Configuration config) {ProtoLog.v(WM_DEBUG_CONFIGURATION, "Sending to proc %s new config %s", mName,config);......mHasCachedConfiguration = false;try {mAtm.getLifecycleManager().scheduleTransaction(thread,ConfigurationChangeItem.obtain(config, mLastTopActivityDeviceId));} catch (Exception e) {Slog.e(TAG_CONFIGURATION, "Failed to schedule configuration change: " + mOwner, e);}
}
App
// ConfigurationController.java
void handleConfigurationChanged(@Nullable Configuration config,@Nullable CompatibilityInfo compat) {......synchronized (mResourcesManager) {......// 先更新Resource中的Configuration的值mResourcesManager.applyConfigurationToResources(config, compat);......}final ArrayList<ComponentCallbacks2> callbacks =mActivityThread.collectComponentCallbacks(false /* includeUiContexts */);freeTextLayoutCachesIfNeeded(configDiff);if (callbacks != null) {final int size = callbacks.size();for (int i = 0; i < size; i++) {ComponentCallbacks2 cb = callbacks.get(i);if (!equivalent) {// 再触发Application/Service/Provider等的回调performConfigurationChanged(cb, config);}}}
}
Activity级别
处理的两种方式
针对Activity级别的处理,relaunch当前Activity或触发当前Actiivty回调onConfigurationChanged方法,只会发生其一。
- app在AndroidManifest.xml中配置对应的configChanges,则触发Activity回调onConfigurationChanged方法
- 否则直接执行relaunch操作
ActivityRelaunchItem
如果app未在AndroidManifest.xml中配置对应的configChanges,则当configuration更改时会触发当前top页面的relaunch,对应的event log如下:
10-10 16:12:05.951 3972 8346 I configuration_changed: 512
10-10 16:12:06.021 3972 8346 I wm_relaunch_resume_activity: [0,237413577,21,com.miui.gallery/.activity.MapActivity,200]
10-10 16:12:06.022 3972 8346 I wm_relaunch_activity: [0,218943334,21,com.miui.gallery/.activity.HomePageActivity,200]
// top resume的页面重建后仍然停留在onResume的状态
10-10 16:12:06.244 20930 20930 I wm_on_top_resumed_lost_called: [237413577,com.miui.gallery.activity.MapActivity,pausing]
10-10 16:12:06.284 20930 20930 I wm_on_paused_called: [0,237413577,com.miui.gallery.activity.MapActivity,performPause,39]
10-10 16:12:06.294 20930 20930 I wm_on_stop_called: [0,237413577,com.miui.gallery.activity.MapActivity,handleRelaunchActivity,2]
10-10 16:12:06.328 20930 20930 I wm_on_destroy_called: [0,237413577,com.miui.gallery.activity.MapActivity,performDestroy,32]
10-10 16:12:06.461 20930 20930 I wm_on_create_called: [0,237413577,com.miui.gallery.activity.MapActivity,performCreate,29]
10-10 16:12:06.599 20930 20930 I wm_on_start_called: [0,237413577,com.miui.gallery.activity.MapActivity,handleStartActivity,135]
10-10 16:12:06.603 20930 20930 I wm_on_resume_called: [0,237413577,com.miui.gallery.activity.MapActivity,RESUME_ACTIVITY,2]
10-10 16:12:06.603 20930 20930 I wm_on_top_resumed_gained_called: [237413577,com.miui.gallery.activity.MapActivity,topWhenResuming]// 可见但是pause的页面重建后最终状态仍然是onPause
10-10 16:12:06.625 20930 20930 I wm_on_stop_called: [0,218943334,com.miui.gallery.activity.HomePageActivity,handleRelaunchActivity,6]
10-10 16:12:06.649 20930 20930 I wm_on_destroy_called: [0,218943334,com.miui.gallery.activity.HomePageActivity,performDestroy,13]
10-10 16:12:06.701 20930 20930 I wm_on_create_called: [0,218943334,com.miui.gallery.activity.HomePageActivity,performCreate,34]
10-10 16:12:06.903 20930 20930 I wm_on_start_called: [0,218943334,com.miui.gallery.activity.HomePageActivity,handleStartActivity,199]
10-10 16:12:06.910 20930 20930 I wm_on_resume_called: [0,218943334,com.miui.gallery.activity.HomePageActivity,LIFECYCLER_RESUME_ACTIVITY,5]
10-10 16:12:06.926 20930 20930 I wm_on_paused_called: [0,218943334,com.miui.gallery.activity.HomePageActivity,performPause,1]
传送一个ClientTransaction到app进程:
- 如果应该resume:ActivityRelaunchItem(处理Activity重建) & ResumeActivityItem (处理最终生命周期)
- 否则:ActivityRelaunchItem & PauseActivityItem
void relaunchActivityLocked(boolean preserveWindow) {......if (andResume) {EventLogTags.writeWmRelaunchResumeActivity(mUserId, System.identityHashCode(this),task.mTaskId, shortComponentName, Integer.toHexString(configChangeFlags));} else {EventLogTags.writeWmRelaunchActivity(mUserId, System.identityHashCode(this),task.mTaskId, shortComponentName, Integer.toHexString(configChangeFlags));}startFreezingScreenLocked(0);try {......final ClientTransactionItem callbackItem = ActivityRelaunchItem.obtain(pendingResults,pendingNewIntents, configChangeFlags,new MergedConfiguration(getProcessGlobalConfiguration(),getMergedOverrideConfiguration()),preserveWindow);final ActivityLifecycleItem lifecycleItem;if (andResume) {lifecycleItem = ResumeActivityItem.obtain(isTransitionForward(),shouldSendCompatFakeFocus());} else {lifecycleItem = PauseActivityItem.obtain();}final ClientTransaction transaction = ClientTransaction.obtain(app.getThread(), token);transaction.addCallback(callbackItem);transaction.setLifecycleStateRequest(lifecycleItem);mAtmService.getLifecycleManager().scheduleTransaction(transaction);} catch (RemoteException e) {ProtoLog.i(WM_DEBUG_STATES, "Relaunch failed %s", e);}......
}
然后根据ResumeActivityItem和PauseActivityItem执行相应的生命周期,一般top Activity执行onResume,底下的执行onPause。
ActivityConfigurationChangeItem
如果没有在功能清单文件配置对应的configChanges,则会触发app当前的Activity执行onConfigurationChanged方法,主要是post一个ActivityConfigurationChangeItem到app进程(区分于进程级别的ConfigurationChangeItem)。
更新top Activity的Configuration
10-10 16:12:05.951 3972 8346 I configuration_changed: 512
10-10 16:12:06.021 3972 8346 I wm_relaunch_resume_activity: [0,237413577,21,com.miui.gallery/.activity.MapActivity,200]
// top resume的页面重建后仍然停留在onResume的状态
10-10 16:12:06.244 20930 20930 I wm_on_top_resumed_lost_called: [237413577,com.miui.gallery.activity.MapActivity,pausing]
10-10 16:12:06.284 20930 20930 I wm_on_paused_called: [0,237413577,com.miui.gallery.activity.MapActivity,performPause,39]
10-10 16:12:06.294 20930 20930 I wm_on_stop_called: [0,237413577,com.miui.gallery.activity.MapActivity,handleRelaunchActivity,2]
10-10 16:12:06.328 20930 20930 I wm_on_destroy_called: [0,237413577,com.miui.gallery.activity.MapActivity,performDestroy,32]
10-10 16:12:06.461 20930 20930 I wm_on_create_called: [0,237413577,com.miui.gallery.activity.MapActivity,performCreate,29]
10-10 16:12:06.599 20930 20930 I wm_on_start_called: [0,237413577,com.miui.gallery.activity.MapActivity,handleStartActivity,135]
10-10 16:12:06.603 20930 20930 I wm_on_resume_called: [0,237413577,com.miui.gallery.activity.MapActivity,RESUME_ACTIVITY,2]
10-10 16:12:06.603 20930 20930 I wm_on_top_resumed_gained_called: [237413577,com.miui.gallery.activity.MapActivity,topWhenResuming]
// ATMS.java
/** Applies latest configuration and/or visibility updates if needed. */
boolean ensureConfigAndVisibilityAfterUpdate(ActivityRecord starting, int changes) {boolean kept = true;final Task mainRootTask = mRootWindowContainer.getTopDisplayFocusedRootTask();// mainRootTask is null during startup.if (mainRootTask != null) {if (changes != 0 && starting == null) {// If the configuration changed, and the caller is not already// in the process of starting an activity, then find the top// activity to check if its configuration needs to change.starting = mainRootTask.topRunningActivity();}if (starting != null) {// 更新top running页面的configurationkept = starting.ensureActivityConfiguration(changes,false /* preserveWindow */);// And we need to make sure at this point that all other activities// are made visible with the correct configuration.// 更新其它可见的Activity的configurationmRootWindowContainer.ensureActivitiesVisible(starting, changes,!PRESERVE_WINDOWS);}}return kept;
}
更新其它可见的Activity的Configuration
// EnsureActivitiesVisibleHelper.java
private void setActivityVisibilityState(ActivityRecord r, ActivityRecord starting,final boolean resumeTopActivity) {.....final boolean reallyVisible = r.shouldBeVisibleUnchecked();......// 如果可见且不是topif (reallyVisible) {.....// First: if this is not the current activity being started, make// sure it matches the current configuration.if (r != mStarting && mNotifyClients) {r.ensureActivityConfiguration(0 /* globalChanges */, mPreserveWindows,true /* ignoreVisibility */);}
10-10 16:12:05.951 3972 8346 I configuration_changed: 512
10-10 16:12:06.022 3972 8346 I wm_relaunch_activity: [0,218943334,21,com.miui.gallery/.activity.HomePageActivity,200]
// 可见但是pause的页面重建后最终状态仍然是onPause
10-10 16:12:06.625 20930 20930 I wm_on_stop_called: [0,218943334,com.miui.gallery.activity.HomePageActivity,handleRelaunchActivity,6]
10-10 16:12:06.649 20930 20930 I wm_on_destroy_called: [0,218943334,com.miui.gallery.activity.HomePageActivity,performDestroy,13]
10-10 16:12:06.701 20930 20930 I wm_on_create_called: [0,218943334,com.miui.gallery.activity.HomePageActivity,performCreate,34]
10-10 16:12:06.903 20930 20930 I wm_on_start_called: [0,218943334,com.miui.gallery.activity.HomePageActivity,handleStartActivity,199]
10-10 16:12:06.910 20930 20930 I wm_on_resume_called: [0,218943334,com.miui.gallery.activity.HomePageActivity,LIFECYCLER_RESUME_ACTIVITY,5]
10-10 16:12:06.926 20930 20930 I wm_on_paused_called: [0,218943334,com.miui.gallery.activity.HomePageActivity,performPause,1]
更新其它不可见Activity的Configuration
其它Activity会在下次resume的时候再触发relaunch,跟上面两个不在同一线程以及同一时间执行,如果想查找relaunch的具体原因,需要向前时间查找对应的configuration_changed的log,具体参考https://blog.csdn.net/xiaoyantan/article/details/126292133
10-11 10:58:57.071 3998 11823 I wm_set_resumed_activity: [0,com.miui.gallery/.activity.HomePageActivity,resumeTopActivity - onActivityStateChanged]
10-11 10:58:57.074 3998 11823 I wm_relaunch_resume_activity: [0,197612339,22,com.miui.gallery/.activity.HomePageActivity,200]