0

I have the following AsyncTask

public class LoadFromCloudTask extends AsyncTask<Void, String, Boolean> {

    public LoadFromCloudTask(LoadFromCloudTaskFragment loadFromCloudTaskFragment) {
        this.loadFromCloudTaskFragment = loadFromCloudTaskFragment;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        ...
        ...
        ...
        // How possible I can halt my execution, prompt for user input, and continue execution based on user input.
        // In Swing, I can ...
        //
        /*
           // Hold the execution, and prompt for user input
           final int result = JOptionPane.showConfirmDialog(LoadFromCloudJDialog.this, message, MessagesBundle.getString("question_title_overwrite_your_data_by_cloud_data_at"), JOptionPane.YES_NO_OPTION);
           // Continue execution based on user input.
         */
    }
}

I was wondering, is it possible to halt AsyncTask execution, prompt for user input and continue execution based on user input?

In Swing, I can achieve this by simply using JOptionPane.showConfirmDialog. How about Android?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

4 Answers4

1

Since doInBackground executes in a different thread, you'll have to pause this thread while showing some dialog in the UI Thread, and once the dialog dismisses you can resume the AsyncTask and read the result.

Jong
  • 9,045
  • 3
  • 34
  • 66
1

Check documentation of the Asynctask , you can cancel and restart the async task, because The task can be executed only once

May you want something like this ideal-way-to-cancel-an-executing-asynctask

doInBackground not execute in UI thread, so you can perform UI operation of showing dialog

Community
  • 1
  • 1
Pragnani
  • 20,075
  • 6
  • 49
  • 74
1

You can achieve this by sending messages to UIThread using publishProgress from inside doInBackground(Params...) and use java's wait/notify mechanisem to wait untel the user enters his input.

A psudo code would look something like this:

 public Object mLock = new Object();
 protected void doInBackground(Params... params){
  /// do your requests.
  publishProgress(Progress ...progress);
  mLock.wait();

 }
 protected void onProgressUpdate(Progress ....progress) {
    // request user input here
 }

and from your UIThread just call mLock.notify() to ask the code to continue.

Also you can perform slimier behavior by splitting your logic across multiple AsynchTasks it will not be very costly because AsynchTasks are managed by a shared (static) ThreadPoolExecutor and a

Mr.Me
  • 9,192
  • 5
  • 39
  • 51
1
You can create pause and resume method inside asyntask as below, and call it according to your convenience.

        public class LoadFromCloudTask extends AsyncTask<Void, String, Boolean> {

            public LoadFromCloudTask(LoadFromCloudTaskFragment loadFromCloudTaskFragment) {
                this.loadFromCloudTaskFragment = loadFromCloudTaskFragment;
            }

    //call this when you want to halt the task        
                    public void pause() {
                           if (this.getStatus().equals(Status.RUNNING)) {
                                   try {
                                           this.wait();
                                   } catch (InterruptedException e) {
                                           e.printStackTrace();
                                   }
                           }
                   }

    // call this when you need to resume the task
                   public void resume() {
                           if (this.getStatus().equals(Status.PENDING)) {
                                   this.notify();
                           }
                   }

            @Override
            protected Boolean doInBackground(Void... params) {
                ...
                ...
                ...
                // How possible I can halt my execution, prompt for user input, and continue execution based on user input.
                // In Swing, I can ...
                //
                /*
                   // Hold the execution, and prompt for user input
                   final int result = JOptionPane.showConfirmDialog(LoadFromCloudJDialog.this, message, MessagesBundle.getString("question_title_overwrite_your_data_by_cloud_data_at"), JOptionPane.YES_NO_OPTION);
                   // Continue execution based on user input.
                 */
            }
        }
Rekha
  • 901
  • 1
  • 11
  • 20