androidandroid-9.0-piebattery-saverandroid-night-mode

Battery saver (and Night mode) on Android 9 Pie relaunches whole activity, how to prevent this?


When I run my app on device (Google Pixel 2) with Android 9 Pie and switch on (or switch off) battery saver, whole activity immediately relaunches. When I switch on battery saver on lower versions of Android like 6, 7 and 8, app continues normally without relaunching. Is it possible to prevent these relaunches on Android 9 too?

I investigated it and found, that when you switch on Battery saver, even new native Night mode is switched on automatically. So I tried to switch on only Night Mode via "Settings - Developer Options" and it relaunched activity in the same way as switching on Battery Saver. So this relaunching can be caused by Night mode.

Note: don't confuse "Night light", "Dark theme" and "Night Mode", these are different things and problem is only with "Night mode".

This is stacktrace from debugger, where can be seen, that OS tries to relaunch activity by stopping it (and then it creates it again).

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)

I read here https://developer.android.com/about/versions/pie/power, that on new Android there are some changes in Battery Saver, but I didn't find any details and any advices for fixing possible problems. Can you please help me?

Actual result: I run my app and switch on/off battery saver -> whole main activity relaunches, so it is stopped and then it tries to run as newly created.

Expected result: I run my app and switch on/off battery saver -> nothing happens with my app, it continues with it's main activity as nothing happened.

Thank you in advance.


Solution

  • 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).