[英]How can I know that a task I started onResume is running?
If I run an async call onResume
is there a good way to detect that if the async call is running to not call it again? 如果我在onResume
运行异步调用,是否有一种很好的方法来检测异步调用是否正在运行以不再再次调用它?
Because I am doing a network call using an AsynTask
in onResume
of a Fragment and it seems to be called too often 因为我正在使用片段的onResume
中的AsynTask
进行网络调用,所以它似乎经常被调用
Update 更新
THe problem is that when onResume
is called there are no variables available. 问题是,当调用onResume
时,没有可用的变量。 So I don't have a valid reference of an async task anymore to check the status. 因此,我不再具有异步任务的有效引用来检查状态。
The code is like: 代码如下:
public void onResume() {
AsynTask<Void, Void, String> theTask = new AsyncTask<>() {
//code to run
};
theTask.execute();
//code
}
So how am I suppose to do this since I don't have a reference? 那么,由于没有参考资料,我应该怎么做? I mean where should I keep theTask
in order to be able to cancel it onResume
? 我的意思是我应该将theTask
保留在哪里才能在onResume
上取消它?
You can getStatus
to get the status of the AsyncTask
. 您可以使用getStatus
获取AsyncTask
的状态。 To check if its running you can compare it to AsyncTask.Status.RUNNING
. 要检查它是否正在运行,可以将其与AsyncTask.Status.RUNNING
进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.