How to run the same asynctask more than once?

前端 未结 12 2089
难免孤独
难免孤独 2020-12-01 03:32

I have my asyncTask run when the activity first starts, then if network connectivity is not available then i have a refresh button that tries to run the asyncTask to try aga

12条回答
  •  甜味超标
    2020-12-01 04:15

    You can do it like this :

    private MyAsyncTask createAsyncTask(){
        if (myAsyncTask == null){
            return myAsyncTask = new MyAsyncTask();
        }
            myAsyncTask.cancel(true);
            return myAsyncTask = new MyAsyncTask();
     }
    

    and then you can use it :

    createAsyncTask().execute();
    

    this make a new instance of your background task everytime.

提交回复
热议问题