4

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.
Vadim Eksler
  • 865
  • 9
  • 24
  • Are multiple activities being used? What launchMode values are used for them? – kirsh300 Sep 30 '20 at 15:59
  • 1
    no, single "main" activity with mode=singleInstance – Vadim Eksler Oct 04 '20 at 05:34
  • 1
    I'm experiencing this [issue too on a Pixel 4XL - Android 11](https://stackoverflow.com/questions/64192177/android-11-foregroundservices-ontaskremoved-is-trigged-when-home-button-press?noredirect=1#comment113531335_64192177) For me it's slightly different. When I launch the app and press home it works as expected, the service does not trigger onTaskRemoved, but if I open another activity within the app then launch home, it's more likely to trigger it. I am not using camera or record permissions but do use location. It even occurs if I open another app then press home. – behelit Oct 05 '20 at 22:36
  • @behelit can you see in your logcat EventSequenceValidator warning? – Vadim Eksler Oct 06 '20 at 06:55
  • Have you found a solution? I have a similar issue. – IgorGanapolsky Oct 06 '20 at 19:04
  • No solutions yet. It's probably an Android 11 OS bug. – behelit Oct 06 '20 at 23:05
  • @VadimEksler yes! I do see that D/EventSequenceValidator: IntentStarted during UNKNOWN. Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=myappname/.tabs.MainActivity } java.lang.Throwable: EventSequenceValidator#getStackTrace at com.google.android.startop.iorap.EventSequenceValidator.logWarningWithStackTrace(... – behelit Oct 06 '20 at 23:06
  • there's lots of them tho, even for pkg=com.google.android.gms – behelit Oct 06 '20 at 23:12

1 Answers1

2

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.

behelit
  • 1,765
  • 2
  • 20
  • 34
  • Yes. It works for me also. It's not what I want but for some time it will help. – Vadim Eksler Oct 07 '20 at 09:40
  • android:launchMode="singleTask" also work for me. The strange thing that if I have some activity that wasn't started but "singleInstance" it still call to onTaskRemoved – Vadim Eksler Oct 07 '20 at 11:28