Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass the result of asynctask onpostexecute method into the parent activity android

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

like image 236
vinuonline Avatar asked Dec 17 '22 02:12

vinuonline


1 Answers

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

like image 167
Marek Sebera Avatar answered May 01 '23 09:05

Marek Sebera