Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value from AsyncTask class onPostExecute method

Ok so now I have Class A that contains some spinners that values will be populated by Class B that extends AsnycTask which grabs the spinner values from a web service. In class B i manage to retrieve the values, showing in a Toast. The problem now is how do I pass those spinner values back to Class A?

I've tried

Can OnPostExcecute method in AsyncTask RETURN values?

by passing Class A to Class B and store the value in a public variable of Class A like below

@Override
protected void onPostExecute(String result)
{
  classA.classAvariable = result;
}

However whenever I try to read the classAvariable i always get a NullPointer Exception. Seems like the variable was never assigned with the result. For readability purpose I needed to seperate Class B instead of using as an inline class.

Any ideas my fellow Java programmers?

like image 735
Jack-V Avatar asked Dec 11 '12 07:12

Jack-V


People also ask

How do I return a value from AsyncTask?

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).

What method must be overridden for using AsyncTask?

AsyncTask must be subclassed to be used. The subclass will override at least one method ( doInBackground(Params...) ), and most often will override a second one ( onPostExecute(Result) .)

How many methods are there for AsyncTask class?

Method of AsyncTask In Android: In Android, AsyncTask is executed and goes through four different steps or method.


2 Answers

I guess you are trying to read class A variable before it is being set.. Try to do it using callbacks..in the callback function pass the values and refresh your spinners..

You can create interface, pass it to AsyncTask (in constructor), and then call method in onPostExecute

For example:

Your interface:

public interface OnTaskCompleted{
    void onTaskCompleted(values);
}

Your Activity:

public YourActivity implements OnTaskCompleted{
    //your Activity
    YourTask  task = new YourTask(this); // here is the initalization code for your asyncTask
}

And your AsyncTask:

public YourTask extends AsyncTask<Object,Object,Object>{ //change Object to required type
    private OnTaskCompleted listener;

    public YourTask(OnTaskCompleted listener){
        this.listener=listener;
    }

    //required methods

    protected void onPostExecute(Object o){
        //your stuff
        listener.onTaskCompleted(values);
    }
}
like image 173
Nermeen Avatar answered Nov 12 '22 05:11

Nermeen


Problem here is that when you execute your AsynchTask, its doInBackground() methode run in separate thread and the thread that have started this AsynchTask move forward, Thereby changes occur on your variable by AsynchTask does not reflect on parent thread (who stated this AsynchTask) immediately.

Example --

class MyAsynchTask
{

doInbackground()
{
a = 2;
}

}


int a = 5;

new MyAsynchTask().execute();

// here a still be 5

like image 27
Mufazzal Avatar answered Nov 12 '22 06:11

Mufazzal