Android Q(10)系统上的异常生命周期事件

记一次诡异的生命周期事件

问题描述:

众所周知,为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) {
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值