Android - onStop() will be called with a delay

痴心易碎 提交于 2019-12-10 02:44:47

问题


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:


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

}



回答3:


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);




回答4:


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.




回答5:


May be emulator is slow today

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

no onPause() or anything is called before this



来源:https://stackoverflow.com/questions/35594464/android-onstop-will-be-called-with-a-delay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!