简体   繁体   中英

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?
Because I am doing a network call using an AsynTask in onResume of a Fragment and it seems to be called too often

Update
THe problem is that when onResume is called there are no variables available. 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 ?

You can getStatus to get the status of the AsyncTask . To check if its running you can compare it to AsyncTask.Status.RUNNING .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM