OnStop () does not receive a call when I press the back button

This is normal.

Docs say

"The methods onStart () and onStop () can be called several times, since the action alternates between visible and hidden for the user"

When I click the back button, it will return to the previous action, which completely covers the old one.

What's going on here?

+6
android
source share
3 answers

You want onPause (), not onStop (). onStop is called just before the action is destroyed when the system is low in memory. onPause is called whenever a user transitions from your activity. See the chart at http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle .

0
source share

onStop() is called every time the action is no longer displayed. Therefore, when the back button is pressed, onStop() actually called.

A simple check is to put breakpoints in the onStop()/onStart() and start a debugging session.

BUT note that onStop() current activity will most likely be called AFTER onStart()/onResume() Activity you are switching to.

Therefore, I think that you tried to update something in onStop() of the 1st activity and expected to receive updated data in onStart() of the 2nd activity, which caused errors.

+2
source share

If you still need those that will be called for your activity instance when creating a new instance, you can use this hacker solution . Just do your logic in #doStopOperation () and #doDestroyOperation () instead of #onStop () and #onDestroy () - and be sure to call super # onStop () and super # onDestroy () from your overridden methods. Obviously, when you expand this activity, you donโ€™t need to extend the AppCompatActivity, you can use the usual activity instead. It works for me in production, so hopefully it helps.

I think you could write a hacker timer to call a stop and destroy, even if a new instance is not created, but it is up to you.

0
source share

All Articles