4

I am developing an application in which i need to send the value of the asynctask's onPostExecute method's result in to the previous activity , ie the activity in which the aync task is being called.pls put some codes. Anyhelp is appreciated

vinuonline
  • 143
  • 2
  • 3
  • 14

5 Answers5

8

Two ways:

  1. Declare class extending AsyncTask as private class in parent Activity
  2. Pass Handler or Activity itself as param of class extending AsyncTask

If I were you, I'd follow the first option.
Look at DOCS:

class MyActivitySubclass extends Activity {

    function runOnPostExecute(){
        // whatever
    }

    private class MyTask extends AsyncTask<Void, Void, Void> { 

        void doInBackground(Void... params){
            // do your background stuff
        }

        void onPostExecute(Void... result){
            runOnPostExecute();
        }

    }

}

Note 1

Code placed in body of function onPostExecute is already run on Activity thread, you should just mention that this keywords leads to MyTask.this and not MyActivitySubclass.this

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
2

Well if your AsyncTask is an inner class, you could simply call a method in your activity from onPostExecute():

public class MyActivity extends Activity {

    public void someMethod(String someParam) {
        // do something with string here
    }

    public class InnerTask extends AsyncTask<...> {

        protected void onPostExecute(result) {
            someMethod(Send parameters);
        }

    }

}
soren.qvist
  • 7,376
  • 14
  • 62
  • 91
1

The onPostExecute method is fired on the main UI thread, so anything done there is already on the AsyncTasks caller.

http://developer.android.com/reference/android/os/AsyncTask.html

Kevin
  • 532
  • 2
  • 7
  • 1
    OP's problem is obviously AsyncTask subclass declared as public class in separate file. He already knows what is `onPostExecute` used for – Marek Sebera Mar 28 '12 at 14:46
1

Fire an event in the OnPostExecute.

Klaasvaak
  • 5,634
  • 15
  • 45
  • 68
0

Its an add on to the answer by Marek Sebera, he pointed to use a handler. To keep the code simple and intuitive use an interface. This isn't alien concept, we use it all the time for callback functions (eg: OnClickListner etc..). The code would look some thing like this.

    public class InnerTask extends AsyncTask<...> 
    {
        interface ResultHandler
        {
            void gotResult(<> result);
        }

        private ResultHandler myResult;

        //constructor
        public InnerTask(....params...,ResultHandler callback)
        {
            ...
            this.myResult = callback;
        }

        protected void onPostExecute(<>result) 
        {
            ...
            myResult.gotResult(result);
        }

    }

    public class MyActivity extends Activity implements InnerTask.ResultHandler
    {
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            //do something
            //if you want the InnerTask to execute here
            InnerTask i = new InnerTask(....params...,this);    //send 'this' as parameter
            i.execute();
        }
        @Override
        public void gotResult(<> result)
        {
            //from onPostExecute
        }
    }

If we want to use the same AsynTask class at multiple sites we can use this type of implementation instead of using nested classes implementation.

Sujay
  • 1
  • 1