Android 尽管没有做任何工作,但 onStop 超时

Android onStop times out despite doing no work

我在返回 activity 时遇到应用程序崩溃问题。

如果我使用主页按钮离开,phone(或模拟器)onPause、onSaveInstanceState 和 onStop 都会根据生命周期调用:

05-05 14:12:20.790 2043-2043/? I/myview: onPause was run
05-05 14:12:20.790 2043-2043/? I/myview: onSaveInstanceState was run
05-05 14:12:20.790 2043-2043/? I/myview: onStop was run

activity中的代码:

@Override
    protected void onPause(){
        super.onPause();
        Log.i("myview", "onPause was run");
    }

    @Override
    protected void onStop(){
        super.onStop();
        Log.i("myview", "onStop was run");
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        // Always call the superclass so it can save the view hierarchy state
        super.onSaveInstanceState(savedInstanceState);
        Log.i("myview", "onSaveInstanceState was run");
    }

然而十秒后报告如下:

05-05 14:12:31.420 1586-1601/? W/ActivityManager: Activity stop timeout for ActivityRecord{9d354dc0 u0 com.example.gavin.youdrowned/.MainActivity t2}
05-05 14:12:31.420 1586-1601/? I/ActivityManager: Activity reported stop, but no longer stopping: ActivityRecord{9d354dc0 u0 com.example.gavin.youdrowned/.MainActivity t2}

我可以导航回 activity,但是没有调用 onResume 或 onCreate。

应用程序按预期全屏运行,直到我尝试与之交互。屏幕上的 Tap/Clicking 导致大约 5 秒后出现以下错误:

05-05 14:12:31.420 1586-1601/? E/ActivityManager: ANR in com.example.gavin.youdrowned (com.example.gavin.youdrowned/.MainActivity)
                                                  PID: 2043
                                                  Reason: Input dispatching timed out (Waiting because the touched window has not finished processing the input events that were previously delivered to it.)
                                                  Load: 0.54 / 0.13 / 0.04
                                                  CPU usage from 2796ms to -2455ms ago:
                                                    86% 2043/com.example.gavin.youdrowned: 11% user + 74% kernel / faults: 193 minor 1 major
                                                    49% 1586/system_server: 0.1% user + 48% kernel / faults: 874 minor
                                                    2% 1162/surfaceflinger: 0% user + 2% kernel / faults: 7 minor
                                                    0.1% 1160/debuggerd: 0.1% user + 0% kernel / faults: 3473 minor 12 major
                                                    0.3% 1165/mediaserver: 0% user + 0.3% kernel / faults: 15 minor
                                                    0.3% 1173/adbd: 0% user + 0.3% kernel / faults: 216 minor
                                                    0.3% 1938/com.google.android.gms: 0% user + 0.3% kernel / faults: 62 minor
                                                    0.1% 1186/logcat: 0% user + 0.1% kernel
                                                    0.1% 1708/com.google.android.gms.persistent: 0% user + 0.1% kernel / faults: 22 minor
                                                    0.1% 1746/com.android.phone: 0.1% user + 0% kernel / faults: 98 minor
                                                   +0% 2373/com.google.android.gms.ui: 0% user + 0% kernel
                                                  71% TOTAL: 7.8% user + 63% kernel
                                                  CPU usage from 1945ms to 2451ms later:
                                                    98% 1586/system_server: 0% user + 98% kernel / faults: 1 minor
                                                      96% 1622/Thread-35: 0% user + 96% kernel
                                                      1.9% 1601/ActivityManager: 0% user + 1.9% kernel
                                                    98% 2043/com.example.gavin.youdrowned: 12% user + 86% kernel / faults: 4 minor
                                                      98% 2088/Thread-62: 12% user + 86% kernel
                                                     +0% 2401/AudioTrack: 0% user + 0% kernel
                                                    1.6% 1162/surfaceflinger: 0% user + 1.6% kernel
                                                  100% TOTAL: 6% user + 94% kernel

onResume 从未被调用,onStop 超时似乎表明触摸事件背后的 ANR 错误不是主要问题,但是 onStop 除了显示它被调用的日志外没有任何作用。

我不确定是什么导致了这个问题,也不知道去哪里调试它,如果有任何见解,我们将不胜感激。

经过几个月的开关电源猜测变化:

holder.getSurface().unlockCanvasAndPost(c);

holder.unlockCanvasAndPost(c);

在管理屏幕更新的代码中似乎已经解决了这个问题。