Android - onStop() will be called with a delay

2020-08-11 10:20发布

问题:

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.

回答1:

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.



回答2:

if u want to fix that problem , just go to the manifest and add noHistory:true to ur activity it will call on Stop directly no more 10sec delay . had the same problem this fixed it , might also be possible to pass it programmatically in a flag if u wish to do so.



回答3:

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

You can use something like eventBus, RxBus to post a event in the

Application.ActivityLifecycleCallbacks {

    //post event here.and check the activity's Name in my targetActivity which play my video.
    onActivityStarted()

}


回答4:

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

In my case, the activity being launched had a fragment, which had a WebView. I have no idea why, but if I removed following line, the delay was only around 2 seconds.

webView.setScrollbarFadingEnabled(false);



回答5:

I have just faced this issues, after debugging there is a case that AdView (Admob banner) causes this issue.

In activity A (which is placed an adview) i start activity B. Back to A from B and onPause of B call immediately but all stop/destroy take 10s to be called. Remove the ad banner then everything get back normal.

Just post this case here for anyone to ref.



回答6:

May be emulator is slow today

but onStop() should be called at once as single command

no onPause() or anything is called before this