疑问背景:
在Android Q高版本,发现经常通过events日志来看Activity生命周期的时候,经常看到如下打印:
06-27 12:02:07.091 4812 4812 I wm_on_top_resumed_gained_called: [227500858,com.android.launcher3.uioverrides.QuickstepLauncher,topStateChangedWhenResumed]
06-27 12:02:07.092 4812 4812 I wm_on_top_resumed_lost_called: [227500858,com.android.launcher3.uioverrides.QuickstepLauncher,topStateChangedWhenResumed]
即有wm_on_top_resumed_gained_called和wm_on_top_resumed_lost_called相关打印,第一次看到时候还是很疑惑这个东西到底是干啥的?resumed怎么会增加和失去?
疑问解答:
1、根据events log线索进行寻找
wm_on_top_resumed_gained_called一般这种tag,在代码中表现一般如下:WmOnToResumeGainedCalled
这里可以寻找到相关的代码:
final void performTopResumedActivityChanged(boolean isTopResumedActivity, String reason) {
onTopResumedActivityChanged(isTopResumedActivity);
if (isTopResumedActivity) {
EventLogTags.writeWmOnTopResumedGainedCalled(mIdent, getComponentName().getClassName(),
reason);
} else {
EventLogTags.writeWmOnTopResumedLostCalled(mIdent, getComponentName().getClassName(),
reason);
}
}
在执行完了onTopResumedActivityChanged后进行的打印。
下面来重点看看onTopResumedActivityChanged
/**
* Called when activity gets or loses the top resumed position in the system.
*
* <p>Starting with {@link android.os.Build.VERSION_CODES#Q} multiple activities can be resumed
* at the same time in multi-window and multi-display modes. This callback should be used
* instead of {@link #onResume()} as an indication that the activity can try to open
* exclusive-access devices like camera.</p>
*
* <p>It will always be delivered after the activity was resumed and before it is paused. In
* some cases it might be skipped and activity can go straight from {@link #onResume()} to
* {@link #onPause()} without receiving the top resumed state.</p>
*
* @param isTopResumedActivity {@code true} if it's the topmost resumed activity in the system,
* {@code false} otherwise. A call with this as {@code true} will
* always be followed by another one with {@code false}.
*
* @see #onResume()
* @see #onPause()
* @see #onWindowFocusChanged(boolean)
*/
public void onTopResumedActivityChanged(boolean isTopResumedActivity) {
}
从注释可以翻译出以下几个点:
1、在android Q以后,多个复合窗口下activity可以多个同时进行resume,即多个activity处于Resumed状态,所以只有onResume已经不能简单满足了
2、这个回调的参数isTopResumedActivity
为true就代表当前哪个activity是处于最最新进行resume的activity,也代表它是最顶部的resumed的activity
为false代表已经不是最顶部的resumed的activity了
3、app可以通过这个回调方便控制一些比如相机获取等,需要把相关资源让给把最顶部resumed的activity
总结:
简单说就是新版本android多个activity可以同时resumed状态了,比如分屏,自由窗口,多屏display等,但是一般最后最顶部resumed的activity获取用户焦点,有了这个回调就方便activity自行控制一些自己的资源释放和获取
更多实战即相关答疑,请关注公众号“千里马学框架”