1

当我在装有 Android 9 Pie 的设备(Google Pixel 2)上运行我的应用程序并打开(或关闭)节电模式时,整个活动会立即重新启动。当我在 6、7 和 8 等较低版本的 Android 上打开省电模式时,应用程序会正常运行而无需重新启动。是否也可以在 Android 9 上阻止这些重新启动?

我对其进行了调查,发现当您打开省电模式时,即使是新的原生夜间模式也会自动打开。因此,我尝试通过“设置 - 开发人员选项”仅打开夜间模式,并以与打开省电模式相同的方式重新启动活动。因此,这种重新启动可能是由夜间模式引起的。

注意:不要混淆“夜灯”、“黑暗主题”和“夜间模式”,这些是不同的东西,问题仅在于“夜间模式”。

这是来自调试器的堆栈跟踪,可以看出,操作系统试图通过停止它来重新启动活动(然后它再次创建它)。

onStop:579, MyActivity (com.mypackage)
callActivityOnStop:1432, Instrumentation (android.app)
performStop:7375, Activity (android.app)
callActivityOnStop:4181, ActivityThread (android.app)
handleRelaunchActivityInner:4796, ActivityThread (android.app)
handleRelaunchActivity:4732, ActivityThread (android.app)
execute:69, ActivityRelaunchItem (android.app.servertransaction)
executeCallbacks:108, TransactionExecutor (android.app.servertransaction)
execute:68, TransactionExecutor (android.app.servertransaction)
handleMessage:1816, ActivityThread$H (android.app)
dispatchMessage:106, Handler (android.os)
loop:193, Looper (android.os)
main:6718, ActivityThread (android.app)
invoke:-1, Method (java.lang.reflect)
run:493, RuntimeInit$MethodAndArgsCaller (com.android.internal.os)
main:858, ZygoteInit (com.android.internal.os)

我在这里读到https://developer.android.com/about/versions/pie/power,在新的 Android 上,省电模式有一些变化,但我没有找到任何细节和解决可能问题的任何建议。你能帮我么?

实际结果:我运行我的应用程序并打开/关闭电池保护程序 - > 整个主要活动重新启动,因此它被停止,然后它尝试以新创建的方式运行。

预期结果:我运行我的应用程序并打开/关闭省电模式 -> 我的应用程序没有任何反应,它继续它的主要活动,因为什么也没发生。

先感谢您。

4

1 回答 1

2

I figured it out by myself, so posting answer here (it is from https://github.com/flutter/flutter/issues/25626:)) To fix this problem, place uiMode in the list of android:configChanges in your AndroidManifest.

           <activity android:name=".MainActivity"
                  android:launchMode="singleTop"
                  android:theme="@android:style/Theme.Black.NoTitleBar"
                  android:configChanges="orientation|keyboardHidden|keyboard|screenSize|uiMode"
                  android:hardwareAccelerated="true"
                  android:windowSoftInputMode="adjustResize">

It is because uiMode value prevents app from restarting because of night mode (or putting in the dock too).

于 2019-04-01T11:40:30.583 回答