Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...
like image 751
yydl Avatar asked May 31 '12 07:05

yydl


People also ask

Is onStop guaranteed to be called?

If you then run this application on an API 10 phone, onStop() is not guaranteed to be called.

What is onStop () meant for?

onStop() method is called after onPause() method when activity goes in background. This method can be used to stop Api calls etc.

Which callback is called when the activity?

onStart() This callback is called when the activity becomes visible to the user. 3. onResume() This is called when the user starts interacting with the application.

What happens when onDestroy is called?

If onDestroy() is called as the result of a configuration change, the system immediately creates a new activity instance and then calls onCreate( ) on that new instance in the new configuration.


2 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.

like image 55
thiagolr Avatar answered Sep 22 '22 19:09

thiagolr


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.

like image 25
Marcino Avatar answered Sep 20 '22 19:09

Marcino