• 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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    现在做跨境电商还需要全球代理IP吗?全球代理IP哪家靠谱?
    sendfile数据copy流程
    LINUX
    业务流程管理BPM到底有什么用
    GO 语言的并发编程相关知识点简介与测试【GO 基础】
    [山东科技大学OJ]2509 Problem A: 整型占用的存储空间
    Java面向对象程序设计期末题库(选择题)
    LCD1602指定位置显示字符串-详细版
    MATLAB中的脚本和函数有什么区别?
    FPGA NVMe SSD SQ
  • 原文地址:https://blog.csdn.net/joye123/article/details/126467241