Android - onStop () will be called with a delay

I found that my onStop() method will be called with a delay of less than 10 seconds. I have never seen this behavior.

Note: - The operation is singleTop and begins with the flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT .

Note: - I am using Build Tools v23.0.2.

There was no delay before, and the method will be called immediately.

+5
source share
5 answers

I assume that you are starting another action and you expect the current activity to receive the onStop () callback. According to the life cycle of the activity, the onPause () method is called before onStop (). In some cases, onSaveInstance () is also called before the onStop () method. In addition, when you call startActivity or startActivityForResult (again, I assume that you expect onStop to be called), depending on the parameters passed, if these parameters need to be calculated / extracted / etc, it may take some time before the system will be able to execute startActivity, which would be the earliest that Android initiates lifecycle calls. In the absence of any code, it is impossible to see what else is being executed before calling onStop. I suggest you check the timeline of the code, starting at startActivity and when onStop is called, perhaps by starting timestamps for each call, starting from the timestamp before calling startActivity, ending with the timestamp at the beginning of onStop to find out where the time was spent. I also suggest simplifying this by making sure that all startActivity or startActivityForResult parameters are previously set to their values, if not already.

+2
source

Maybe the emulator is slow today

but onStop () should be called immediately as a single command

no onPause () or anything called before this

0
source

In my case, I stop the video player when calling onStop() , but calling onStop() has a delay of 10 s.

You can use something like eventBus, RxBus to host an event in

 Application.ActivityLifecycleCallbacks { //post event here.and check the activity Name in my targetActivity which play my video. onActivityStarted() } 
0
source

I had the same problem. When I started with startActivity (), onStop () was called exactly 10 seconds after onPause ().

In my case, there was a fragment in a running activity that had a WebView. I have no idea why, but if I deleted the next line, the delay was only about 2 seconds.

webView.setScrollbarFadingEnabled(false);

0
source

If you want to fix this problem, just go to the manifest and add noHistory: true for ur activity, it will cause Stop directly for no more than 10 second delay. had the same problem as fixed it, you could also pass it programmatically in the flag if you want to do this.

0
source

Source: https://habr.com/ru/post/1243729/


All Articles