Home » Android » android – Why is onStop being called right after my Activity is launched?

android – Why is onStop being called right after my Activity is launched?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have an activity that needs to turn on the screen when it is started (just in case the screen was off). So in my onCreate, I have:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            |WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            |WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            |WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

Using this straightforward combination, I am able to cause my activity to display whenever it is started from my background service (yes, this is a legitimate case).

The problem, however, is that there is a very strange lifecycle behavior when I start up my activity in such a case. Using extensive logging, I was able to figure out that the following 7-step process happens immediately after the activity is started:

  1. onCreate
  2. onStart
  3. onResume
  4. onPause
  5. onStop
  6. onStart
  7. onResume

See that? For a simple activity start, onStart is called twice. And more importantly, onStop is mysteriously called, even though the activity was just started – and nothing happened that would cause it to stop.

I have tested this in many different scenarios and it appears that this strange behavior only happens when the screen is off and the activity is launched after it was destroyed. If the screen is on, or if the activity was stopped [but not yet destroyed] the activity launches normally, and onStart is only called once.

Bottom line: it appears that when my activity is launched and the screen is forced on, Android starts the activity, then stops it, and then starts it again for no apparent reason.

So: why is this happening? And is there anything I can do to workaround this (so that onStop isn’t called until there is a legitimate cause for it to be)?


Notes:

  • The activity in question is using the singleTask launchmode
  • I have tried disabling the keyguard / lock but it has no effect
  • I am witnessing this behavior on a Samsung Galaxy Tab 10.1 running Android 3.2. I have not tested whether this applies to anything else…
How to&Answers:

I had a similar problem here:
Activity Lifecycle X Power Button X Lock Screen

The problem was that as my activity was forced landscape, when I turned on the screen it showed the lock screen in portrait and it was causing a configuration change and hence destroying the current activity.

The solution was to add a android:configChanges=”orientation” on the Activity in my AndroidManifest.xml.

Answer:

As suggested by @cyberhuman, the answer to this question OnPause and OnStop() called immediately after starting activity pointed me to the right direction.

My issue with the activity completing a full ‘dummy’ lifecycle before being actually displayed to the user was that my attempt to play the ringtone when the activity became visible was directly muted by the additional, subsequent “onStop()”, and using flags to make the activity behave properly when launched when screen is off/when screen is locked/when screen is on/when activity is running in the background was not possible.

I finally solved my issue by overriding the onWindowFocusChanged(boolean hasFocus) method and launching the ringtone from there. Putting it here in case somebody has the same issue.

Answer:

You could check for the situation in onStart, set a static or global variable, and then check the variable in onStop to override the standard behavior.