Android-onStop()将被延迟调用 [英] Android - onStop() will be called with a delay

查看:595
本文介绍了Android-onStop()将被延迟调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我的活动onStop()方法将在不到10秒的延迟内被调用.我从未见过这种行为.

I found my activities onStop() method will be called with a less than 10 seconds delay. I've never seen before this behavior.

注意:-活动为singleTop,它以Intent.FLAG_ACTIVITY_REORDER_TO_FRONT标志开头.

Note :- The activity is singleTop and it starts with Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag.

注意:-:我正在使用Build Tools v23.0.2.

Note :- I'm using Build Tools v23.0.2.

没有延迟,该方法将立即被调用.

The delay wasn't before and the method would be called immediately.

推荐答案

我猜您正在启动另一个活动,并且您希望当前活动收到onStop()回调.根据活动生命周期,onPause()方法在onStop().在某些情况下,还会在onStop()方法之前调用onSaveInstance().另外,当您调用startActivity或startActivityForResult时(同样,我假设这就是为什么您希望调用onStop的原因),具体取决于传递的参数,如果这些参数需要计算/获取/等,则可能在系统执行startActivity之前需要花一些时间,这将是Android最早启动生命周期调用的时间.在这里没有任何代码的情况下,无法在调用onStop之前查看正在执行的其他操作.我建议您检查代码执行时间的时间轴,从startActivity开始,并在调用onStop时,可能是通过记录每个调用的时间戳记,从startActivity调用之前的时间戳记开始,直到在startActivity调用开始时的时间戳记结束onStop,看看时间在哪里.我还建议通过确保将startActivity或startActivityForResult的所有参数预先设置为它们的值来简化此操作(如果还不是这种情况的话).

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.

这篇关于Android-onStop()将被延迟调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆