Android AsyncTask - Order of execution

前端 未结 6 1141
醉酒成梦
醉酒成梦 2020-12-30 07:58

I am facing an issue regarding the order of exection of AsyncTasks.

My question is :

Assuming I have 2 implementations of AsyncTask : MyAsyncTask1

相关标签:
6条回答
  • 2020-12-30 08:39

    Call 2nd Task in onPostExecute of task 1 as

    @Override
    protected void onPostExecute(String string){//String or whatever u passed
        new MyAsyncTask2 ().execute ();
    }
    
    0 讨论(0)
  • 2020-12-30 08:41

    In order to "chain" any number of AsyncTasks, what I do is have my AsyncTasks recieve a custom Callback as a parameter. You just have to define it like this:

    public interface AsyncCallback(){
        public void onAsyncTaskFinished();
    }
    

    Your AsyncTask implementation constructor should have an AsyncCallback as a parameter, like this:

    private AsyncCallback callback;
    
    public AsyncTaskImplementation(AsyncCallback callback){
        //Do whatever you're doing here
        this.callback = callback;
    }
    

    Of course if they have more parameters, don't delete them.

    Then, just before the end of onPostExecute, introduce this:

    if (callback != null) callback.onAsyncTaskFinished();
    

    So, if you pass a callback, the AsyncTask will execute your callback in the main thread. If you don't want to execute any callback, just pass null

    So, to call your AsyncTask chain you just write this:

    new AsyncTask1(new AsyncCallback(){
    
        public void onAsyncTaskFinished(){
             new AsyncTask2(null).execute();
        }
    
    }).execute();
    

    I hope this helps you ;-)

    0 讨论(0)
  • 2020-12-30 08:43

    Don't make things complicated and try to keep your design simple and clean. The race between your both AsyncTasks depends on the amount of processing they carry inside their overrided methods. Logically, if you want your AsyncTask1 to complete before AsyncTask2, then simply add the code-blocks of your second Async inside the first one.

    Or launch your second Async from the onPostExecute of your first Async - but personally, that seems unnecessary.

    0 讨论(0)
  • 2020-12-30 08:48

    The only way to ensure that two threads (that's what AsyncTasks basically are) are executed in the order you want, is to start the second thread when the first one finishes.

    In your case, to keep implementation abstracted and not have to actually call AsyncTask2 in the onPostExecute of AsyncTask1 (the way Anup and Sanket suggested, which is also fine if you want to mix them), make AsyncTask1 call super.executeAsyncTask2(), where executeAsyncTask2() is a method in your CustomActivity which starts the second AsyncTask

    0 讨论(0)
  • 2020-12-30 08:54

    Call your second Async task at the end of the first Aysnc task (i.e at the end of the onPostExecute() method of your first async task)

    //first async task's onPostExecute method
    
    onPostExecute()
    {
    //do your stuff and call second async task
    
    new MyAsyncTask2 ().execute ();
    
    }
    

    This will guarantee execution of the second task after the first one in a simple and elegant fashion.

    0 讨论(0)
  • 2020-12-30 08:55

    AsyncTask execute in background so in your case first code will start task1 and immediately start task2.. so call

    new MyAsyncTask2 ().execute ();
    

    this in to postexecute of Task1

    0 讨论(0)
提交回复
热议问题