众所周知,为Activity设置为透明窗口主题会影响前一个Activity的生命周期,也就是在Activity的主题中设置windowIsTranslucent属性为true。
例如
第一步:
ActivityA打开ActivityB,其中ActivityB的windowIsTranslucent设置为true。ActivityA的生命周期会执行onPause,但是不会执行onStop,因为系统认为此时ActivityA是可见的,只是不能操作。
第二步:
假如在打开ActivityB后,点击Home键将应用退到后台,此时ActivityA的生命周期从onPause变为了onStop。
第三步:
点击应用图标,应用从后台切换回前台,ActivityA执行onStart,但是不会执行onResume。
第四步:
再次点击Home键将应用退到后台,ActivityA的生命周期从onStart直接变成了onStop。
在Android Q (10)系统上,上述的第四步中的ActivityA的生命周期会依次执行onResume -> onPause -> onStop。这就会导致依赖ActivityA生命周期的功能(如埋点),就会重复执行。
系统Bug。问题链接如下https://issuetracker.google.com/issues/185693011
When the life cycle of activity stay in START state and plan to STOP
state soon. We can jump to the STOP state directly instead of going through
the RESUME and PAUSE state. Basically, applications like to do things on
RESUME state, we don’t need to let application to handle the case because
it already plan to STOP.
// android/app/servertransaction/TransactionExecutorHelper.java
mLifecycleSequence.clear();
if (finish >= start) {
- // just go there
- for (int i = start + 1; i <= finish; i++) {
- mLifecycleSequence.add(i);
+ if (start == ON_START && finish == ON_STOP) {
+ // A case when we from start to stop state soon, we don't need to go
+ // through the resumed, paused state.
+ mLifecycleSequence.add(ON_STOP);
+ } else {
+ // just go there
+ for (int i = start + 1; i <= finish; i++) {
+ mLifecycleSequence.add(i);
+ }
}
} else { // finish < start, can't just cycle down
if (start == ON_PAUSE && finish == ON_RESUME) {