Search code examples
androidandroid-activityandroid-10.0

Android Activity onStart called while app is in the background?


I've encountered a strange behavior that only seems to be happening on android 10+:

While my activity is in the background, I will open Android Settings, go to System -> Gestures -> System Navigation -> switch between Gesture Navigation & 2-button Navigation.

When I switch these settings, my app (which is running in the background) recreates my activity, and this new activity receives a call to onStart, then immediately after onStop.

BTW, this behavior is 100% reproducible in this settings flow, but can also occur seemingly at random while the app is in the background.

From my understanding, activities should never be getting a call to onStart while they're still in the background, even if they are being destroyed by the OS for whatever reason... or do I have that wrong?

Due to certain functionalities in my app using 3rd party libraries that are called in onStart, this behavior causes serious problems when onStart is called when the activity is actually in the background.

Has anyone else experienced this? Other than adding delay logic in my onStart code (then aborting if followed by an immediate onStop), is there anyway to prevent this from happening?


Solution

  • onStart() will be called on apps in the background. There isn't anything that says that Android will not do this. The only thing you can really rely on is onResume() and onPause(). onResume() is called when the Activity is in the foreground and has the user's focus. onPause() is called when the Activity had the user's focus and Android is giving the user's focus to another Activity.

    It looks like, in your case, Android is causing a configuration change to be propagated to all apps, which may be why you see the background apps being "woken up" like this, but this is just a guess from me.