Configuration Change派发到App进程

2023-10-12 00:36

本文主要是介绍Configuration Change派发到App进程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

整体时序

在这里插入图片描述
在这里插入图片描述

// 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]

在这里插入图片描述

这篇关于Configuration Change派发到App进程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/191974

相关文章

[Linux]:进程(下)

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:Linux学习 贝蒂的主页:Betty’s blog 1. 进程终止 1.1 进程退出的场景 进程退出只有以下三种情况: 代码运行完毕,结果正确。代码运行完毕,结果不正确。代码异常终止(进程崩溃)。 1.2 进程退出码 在编程中,我们通常认为main函数是代码的入口,但实际上它只是用户级

fzu 2277 Change 线段树

Problem 2277 Change Time Limit: 2000 mSec    Memory Limit : 262144 KB  Problem Description There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each node has a value ai.

java 进程 返回值

实现 Callable 接口 与 Runnable 相比,Callable 可以有返回值,返回值通过 FutureTask 进行封装。 public class MyCallable implements Callable<Integer> {public Integer call() {return 123;}} public static void main(String[] args

C#关闭指定时间段的Excel进程的方法

private DateTime beforeTime;            //Excel启动之前时间          private DateTime afterTime;               //Excel启动之后时间          //举例          beforeTime = DateTime.Now;          Excel.Applicat

linux中使用rust语言在不同进程之间通信

第一种:使用mmap映射相同文件 fn main() {let pid = std::process::id();println!(

Golang进程权限调度包runtime

关于 runtime 包几个方法: Gosched:让当前线程让出 cpu 以让其它线程运行,它不会挂起当前线程,因此当前线程未来会继续执行GOMAXPROCS:设置最大的可同时使用的 CPU 核数Goexit:退出当前 goroutine(但是defer语句会照常执行)NumGoroutine:返回正在执行和排队的任务总数GOOS:目标操作系统NumCPU:返回当前系统的 CPU 核数量 p

如何保证android程序进程不到万不得已的情况下,不会被结束

最近,做一个调用系统自带相机的那么一个功能,遇到的坑,在此记录一下。 设备:红米note4 问题起因 因为自定义的相机,很难满足客户的所有需要,比如:自拍杆的支持,优化方面等等。这些方面自定义的相机都不比系统自带的好,因为有些系统都是商家定制的,难免会出现一个奇葩的问题。比如:你在这款手机上运行,无任何问题,然而你换一款手机后,问题就出现了。 比如:小米的红米系列,你启用系统自带拍照功能后

MFC中App,Doc,MainFrame,View各指针的互相获取

纸上得来终觉浅,为了熟悉获取方法,我建了个SDI。 首先说明这四个类的执行顺序是App->Doc->Main->View 另外添加CDialog类获得各个指针的方法。 多文档的获取有点小区别,有时间也总结一下。 //  App void CSDIApp::OnApp() {      //  App      //  Doc     CDocument *pD

flume系列之:记录一次flume agent进程被异常oom kill -9的原因定位

flume系列之:记录一次flume agent进程被异常oom kill -9的原因定位 一、背景二、定位问题三、解决方法 一、背景 flume系列之:定位flume没有关闭某个时间点生成的tmp文件的原因,并制定解决方案在博主上面这篇文章的基础上,在机器内存、cpu资源、flume agent资源都足够的情况下,flume agent又出现了tmp文件无法关闭的情况 二、

C++编程:ZeroMQ进程间(订阅-发布)通信配置优化

文章目录 0. 概述1. 发布者同步发送(pub)与订阅者异步接收(sub)示例代码可能的副作用: 2. 适度增加缓存和队列示例代码副作用: 3. 动态的IPC通道管理示例代码副作用: 4. 接收消息的超时设置示例代码副作用: 5. 增加I/O线程数量示例代码副作用: 6. 异步消息发送(使用`dontwait`标志)示例代码副作用: 7. 其他可以考虑的优化项7.1 立即发送(ZMQ_IM