Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - onStop() will be called with a delay

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 with Intent.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.

like image 292
Alex Avatar asked Feb 24 '16 06:02

Alex


People also ask

When onStop method is called in Android?

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.

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.

Is onStop always called?

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() .

When onDestroy () is called before onPause () and onStop () in an Android application?

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.


2 Answers

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.

like image 182
Kaamel Avatar answered Oct 05 '22 11:10

Kaamel


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.

like image 40
Jean Raymond Daher Avatar answered Oct 05 '22 13:10

Jean Raymond Daher