开发者

onStop() not getting called when I press the back button

开发者 https://www.devze.com 2023-01-08 12:28 出处:网络
Is this normal. The docs say \"The onStart() and onStop() methods can be called multiple times, as the activity alternates between being visible and hidden to the user\"

Is this normal.

The docs say

"The onStart() and onStop() methods can be called multiple times, as the activity alternates between being visible and hidden to the user"

When I press the back button an it will go back to the pr开发者_开发问答evious activity which totally covers the old one.

What is going on here?


onStop() is called each time Activity is no longer visible. So when back button is pressed onStop() is actually called.

Easy check, - need to put break points in onStop()/onStart() callbacks and run debugging session.

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

Hence, I think you were trying to update something in onStop() of 1st Activity and were expecting to fetch updated data in onStart() of 2nd Activity which caused errors.


If you still need those to get called for your activity instance when a new instance is created, you can use this hacky solution. Just implement 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 extending this activity, you don't need to extend the AppCompatActivity, you can use the regular activity instead. This works for me in a production build, so hopefully it will help.

I guess you could write a timer hack as well to invoke stop and destroy even when no new instance is created, but that's up to you.


You want onPause(), not onStop(). onStop is called just before the activity is destoryed, when the system is low on memory. onPause is called whenever the user navigates away from your activity. See diagram at http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle.

0

精彩评论

暂无评论...
验证码 换一张
取 消