Return a value from AsyncTask in Android

JavaAndroidAsynchronousAndroid AsynctaskReturn

Java Problem Overview


One simple question: is it possible to return a value in AsyncTask?

//AsyncTask is a member class
private class MyTask extends AsyncTask<Void, Void, Void>{

    protected Void doInBackground(Void... params) {
         //do stuff
         return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        //do stuff
        //how to return a value to the calling method?
    }
}

And then within my Activity/Fragment:

// The task is started from activity
myTask.execute()
// something like this?
myvalue = myTask.getvalue() 

EDIT: This was asked a long time ago where I wasn't familiar with Java, now that I'm better with it, I 'll do a quick summary:

The point of async task is that the task is asynchronous, meaning that after you call execute() on the task, the task starts running on a thread of its own. returning a value from asynctask would be pointless because the original calling thread has already carried on doing other stuff (thus the task is asynchronous).

Think of time: At one point of time, you started a task that will run in parallel with the main thread. When the parallel-running task completed, time has also elapsed on the main thread. The parallel task cannot go back in time to return a value to the main thread.

I was coming from C so I didn't know much about this. But it seems that lots of people are having the same question so I thought I would clear it up a bit.

Java Solutions


Solution 1 - Java

That's what onPostExecute() is for. It runs on the UI thread and you can deliver your result from there to the screen (or anywhere else you need). It won't be called until the final result is available. If you want to deliver intermediate results, take a look at onProgressUpdate()

Solution 2 - Java

Why not call a method that handles the value?

public class MyClass extends Activity {
    
    private class myTask extends AsyncTask<Void, Void, Void> {
        
        //initiate vars
        public myTask() {
            super();
            //my params here
        }

        protected Void doInBackground(Void... params) {
            //do stuff
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            //do stuff
            myMethod(myValue);
        }
    }

    private myHandledValueType myMethod(Value myValue) {
        //handle value 
        return myHandledValueType;
    }
}

Solution 3 - Java

Easiest way is to pass the calling object into the async task (upon constructing it if you like):

public class AsyncGetUserImagesTask extends AsyncTask<Void, Void, Void> {

	private MyImagesPagerFragment mimagesPagerFragment;
    private ArrayList<ImageData> mImages = new ArrayList<ImageData>();

	public AsyncGetUserImagesTask(MyImagesPagerFragment imagesPagerFragment) {
		this.mimagesPagerFragment = imagesPagerFragment;
	}

	@Override
	public Void doInBackground(Void... records) {
        // do work here
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		mimagesPagerFragment.updateAdapter(mImages);
	}
}

And the in the calling class (your activity or fragment) execute the task:

public class MyImagesPagerFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        AsyncGetUserImagesTask mGetImagesTask = new AsyncGetUserImagesTask(this);
        mGetImagesTask.execute();
    }

And then the onPostExecuteMethod will call any method on your originating class you like, eg:

    public void updateAdapter(List<ImageData> images) {
        mImageAdapter.setImages(images);
        mImageAdapter.notifyDataSetChanged();
    }
}

Solution 4 - Java

Code Example: Activity uses AsyncTask to get a value in a background thread, then AsyncTask returns the result back to the Activity by calling processValue:

public class MyClass extends Activity {
  private void getValue() {
      new MyTask().execute();
  }

  void processValue(Value myValue) {
     //handle value 
     //Update GUI, show toast, etc..
  }

  private class MyTask extends AsyncTask<Void, Void, Value> {
    @Override
    protected Value doInBackground(Void... params) {
      //do stuff and return the value you want 
      return Value;
    }

    @Override
    protected void onPostExecute(Value result) {
      // Call activity method with results
      processValue(result);
    }
  }
}

Solution 5 - Java

you can try this one: myvalue = new myTask().execute().get(); minus is it will freeze process until asyncron will not be finished ;

Solution 6 - Java

You need to use "protocols" to delegate or provide data to the AsynTask.

Delegates and Data Sources

A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program. (Apple definition)

protocols are interfaces that define some methods to delegate some behaviors.


DELEGATE: Capturate events from the object in background thread


AsynkTask:

public final class TaskWithDelegate extends AsyncTask<..., ..., ...> {
	//declare a delegate with type of protocol declared in this task
	private TaskDelegate delegate;
	
	//here is the task protocol to can delegate on other object
	public interface TaskDelegate {
		//define you method headers to override
		void onTaskEndWithResult(int success);
		void onTaskFinishGettingData(Data result);
	}

	@Override
	protected Integer doInBackground(Object... params) {
		//do something in background and get result
		if (delegate != null) {
            //return result to activity
			delegate.onTaskFinishGettingData(result);
		}	
	}

	@Override
	protected void onPostExecute(Integer result) {
		if (delegate != null) {
            //return success or fail to activity
			delegate.onTaskEndWithResult(result);
		}	
	}
}

Activity:

public class DelegateActivity extends Activity implements TaskDelegate {
    void callTask () {
        	TaskWithDelegate task = new TaskWithDelegate;
        //set the delegate of the task as this activity
        task.setDelegate(this);
    }

    //handle success or fail to show an alert...
	@Override
    void onTaskEndWithResult(int success) {
    
    }

	//handle data to show them in activity...
	@Override
	void onTaskFinishGettingData(Data result) {

	}
}

EDIT: if you call delegate in doInBackground, and the delegate try to edit some view, that will crash because view can be manipulated only by the main thread.


// this refers to Activity
this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
    // Here you can edit views when task notify some changes from background thread
        textView.setText(someValue);
    }
});

EXTRA

DATASOURCE: Provide data to the object in background thread


AsyncTask:

public final class TaskWithDataSource extends AsyncTask<..., ..., ...> {
	//declare a datasource with type of protocol declared in this task
	private TaskDataSource dataSource;
	private Object data;
	
	//here is the task protocol to can provide data from other object
	public interface TaskDataSource {
		//define you method headers to override
		int indexOfObject(Object object);
		Object objectAtIndex(int index);
	}

	@Override
	protected void onPreExecute(Integer result) {
		if (dataSource != null) {
            //ask for some data
			this.data = dataSource.objectAtIndex(0);
		}	
	}

	@Override
	protected Integer doInBackground(Object... params) {
		//do something in background and get result
		int index;
		if (dataSource != null) {
            //ask for something more
			index = dataSource.indexOfObject(this.data);
		}	
	}
}

Activity:

public class DataSourceActivity extends Activity implements TaskDataSource {
    void callTask () {
        	TaskWithDataSource task = new TaskWithDataSource;
        //set the datasource of the task as this activity
        task.setDataSource(this);
    }

    //send some data to the async task when it is needed...
	@Override
    Object objectAtIndex(int index) {
        return new Data(index);
    }

	//send more information...
	@Override
	int indexOfObject(Object object) {
        return new object.getIndex();
	}
}

Solution 7 - Java

Use generic parameters

AsyncTask<Params, Progress, Result>
  • Params — task's input data type
  • Progress — how to inform the world about progress
  • Result — task's output data type

Think like

Result = task(Params)

Example

Load YourObject by string URL:

new AsyncTask<String, Void, YourObject>()
{
    @Override
    protected void onPreExecute()
    {
        /* Called before task execution; from UI thread, so you can access your widgets */

        // Optionally do some stuff like showing progress bar
    };

    @Override
    protected YourObject doInBackground(String... url)
    {
        /* Called from background thread, so you're NOT allowed to interact with UI */

        // Perform heavy task to get YourObject by string
        // Stay clear & functional, just convert input to output and return it
        YourObject result = callTheServer(url);
        return result;
    }

    @Override
    protected void onPostExecute(YourObject result)
    {
        /* Called once task is done; from UI thread, so you can access your widgets */

        // Process result as you like
    }
}.execute("http://www.example.com/");

Solution 8 - Java

For you get the result from a AsyncTask it needs to do it after the super.onPostExcecute(result); Because this means that the background and the AsyncTask also have finished. e.g.:

... into your async task

@Override
protected void onPostExecute(MyBeanResult result) {
    if (dialog.isShowing()) {
        dialog.dismiss();
    }
    if (mWakeLock.isHeld()) {
        mWakeLock.release();
    }
    super.onPostExecute(result);
    MyClassName.this.checkResponseForIntent(result);
}

and the methodr checkResponseForIntent might by something like this:

private void checkResponseForIntent(MyBeanResult response) {
	if (response == null || response.fault != null) {
		noServiceAvailable();
		return;
	}
 ... or do what ever you want, even call an other async task...

I had this problem using .get() from AsyncTask and the ProgressBar simply doesn't work with get(), actually, it only works once that doInBackground finishes.

I hope that it helps you.

Solution 9 - Java

Pass the MainActivity to Async class, So you'll access the MainActivity functions in inner class, This works fine for me:

public class MainActivity extends ActionBarActivity {

   void callAsync()
   {
      Async as = new Async(this,12,"Test");
      as.execute();
   }

   public void ReturnThreadResult(YourObject result)
   {
     // TO DO:
     // print(result.toString());
      
   }
}


public class Async extends AsyncTask<String, String, Boolean> {
    MainActivity parent;
    int param1;
    String param2;

    public Async(MainActivity parent,int param1,String param2){
       this.parent = parent;
       this.param1 = param1;
       this.param2 = param2;
    }

    @Override
    protected void onPreExecute(){};

    @Override
    protected YourObject doInBackground(String... url)
    {
        return result;
    }

    @Override
    protected void onPostExecute(YourObject result)
    {
       // call an external function as a result 
       parent.ReturnThreadResult(result);
    }
  }  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontom91136View Question on Stackoverflow
Solution 1 - JavaTed HoppView Answer on Stackoverflow
Solution 2 - JavaababzyView Answer on Stackoverflow
Solution 3 - JavaSheamus O'HalloranView Answer on Stackoverflow
Solution 4 - Javajt-gilkesonView Answer on Stackoverflow
Solution 5 - JavaEugenUngureanView Answer on Stackoverflow
Solution 6 - JavaIgniteCodersView Answer on Stackoverflow
Solution 7 - JavaNikita BosikView Answer on Stackoverflow
Solution 8 - JavaKlaus VillacaView Answer on Stackoverflow
Solution 9 - JavaHamidView Answer on Stackoverflow