Search code examples
androidandroid-serviceandroid-api-levelsandroid-11

Android 11 (API level 30) onTaskRemoved fired in Foreground service when app go to background


I tried to test app that I work with on android 11 (got it on Pixel 3 XL). I receive strange behaviour when app goes to background: onTaskRemoved fired on Foreground service and looks like app killed and restarted.

Info about Foreground service: in Manifest:

<service
    android:name=".MyFS"
    android:foregroundServiceType="mediaProjection"
    android:enabled="true"
    android:exported="false"/>

in FS class onStartCommand -> return START_STICKY;

Additional info: App using "camera" & "microphone" and I tried to add this two to android:foregroundServiceType="mediaProjection|camera|microphone" but it didn't help.

Also interesting that onTaskRemoved fired just on first time that app goes to Background, if I back to app and click "home" second time onTaskRemoved not called.

Please help me to understand from were it comes. Thanks

PS: Is it related? Before onTaskRemoved I can see in the full logcat this:

2020-10-05 09:33:19.866 1463-1524/? D/EventSequenceValidator: onIntentFailed during UNKNOWN.
    java.lang.Throwable: EventSequenceValidator#getStackTrace
        at com.google.android.startop.iorap.EventSequenceValidator.logWarningWithStackTrace(EventSequenceValidator.java:260)
        at com.google.android.startop.iorap.EventSequenceValidator.onIntentFailed(EventSequenceValidator.java:130)
        at com.android.server.wm.LaunchObserverRegistryImpl.handleOnIntentFailed(LaunchObserverRegistryImpl.java:147)
        at com.android.server.wm.LaunchObserverRegistryImpl.lambda$KukKmVpn5W_1xSV6Dnp8wW2H2Ks(Unknown Source:0)
        at com.android.server.wm.-$$Lambda$LaunchObserverRegistryImpl$KukKmVpn5W_1xSV6Dnp8wW2H2Ks.accept(Unknown Source:2)
        at com.android.internal.util.function.pooled.PooledLambdaImpl.doInvoke(PooledLambdaImpl.java:264)
        at com.android.internal.util.function.pooled.PooledLambdaImpl.invoke(PooledLambdaImpl.java:201)
        at com.android.internal.util.function.pooled.OmniFunction.run(OmniFunction.java:97)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.os.HandlerThread.run(HandlerThread.java:67)
        at com.android.server.ServiceThread.run(ServiceThread.java:44)
2020-10-05 09:33:19.866 1463-1524/? D/EventSequenceValidator: dec AccIntentStartedEvents to 2
2020-10-05 09:33:19.870 27662-27662/: t:main onTaskRemoved here.

Solution

  • For me, changing the activity launch mode away from singleInstance resolved the problem and onTaskRemoved is no longer called. I had 2 activities declared in the manifest with singleInstance. After changing them to singleTop the problem went away.

    Change

            android:launchMode="singleInstance"
    

    to

            android:launchMode="singleTop"
    

    or remove it altogether

    Obviously there are valid reasons for having singleInstance and this is still unexpected behaviour but for now it's a valid workaround.