I found my activities onStop()
method will be called with a less than 10 seconds delay. I've never seen before this behavior.
Note :- The activity is
singleTop
and it starts withIntent.FLAG_ACTIVITY_REORDER_TO_FRONT
flag.Note :- I'm using Build Tools v23.0.2.
The delay wasn't before and the method would be called immediately.
OnStop is called when FirstActivity calls SecondActivity and FirstActivity looses visibility. If Second Activity has a transparent background then the FirstActivity will be visible underneath, so not loosing visibility and onStop on FirstActivity will never be called.
onStop() method is called after onPause() method when activity goes in background. This method can be used to stop Api calls etc.
Since onStop() is not guaranteed to be called, you can't always do in onStop() what is done in onPause(). In most Activities, you will find that you will need to put code in onResume() and onPause() . You usually don't have to do anything in onStop() , onStart() or onRestart() .
onPause() and onStop() will not be invoked if finish() is called from within the onCreate() method. This might occur, for example, if you detect an error during onCreate() and call finish() as a result. In such a case, though, any cleanup you expected to be done in onPause() and onStop() will not be executed.
I am guessing that you are starting another activity and you expect the current activity to receive an onStop() callback. According to the activity lifecycle, the onPause() method is called before the onStop(). In some cases onSaveInstance() is also called before the onStop() method. Additionally, when you call the startActivity or the startActivityForResult (again, I am assuming that is why you expect the onStop to be called), depending on the parameters that are passed, if those parameters need to be calculated/fetched/etc, it may take a while before the system can execute the startActivity, which would be the earliest that Android would initiate the life cycle calls. In the absence of any code here, it is not possible to see what else is getting executed before the onStop is called. I suggest you check the timeline for the code execution time, starting with the startActivity and when onStop is called, maybe by logging the timestamps for each call, starting with the timestamp just before the startActivity call, ending in the timestamp at the start of the onStop, to see where time is spent. I also suggest to simplify this by making sure that all parameters to the startActivity or startActivityForResult are previously set to their values, if that is not already the case.
You can fix the problem by either adding noHistory:true to the activity in the manifest file or by setting the flag programmatically. I had the same problem and this fixed it for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With