Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android wait till async task has finished to do specific method

I have been searching for the answer but no one really answers because there is no point using async task for that. In Android API 11 or above it will force to code it you do network requests on the main thread so I have to do async task. So, here is the question: Is it possible to wait untill the async task is finished then continue? Because I need the data to be there for the calling method etc.

Here is my code:

public JSONObject getJSONFromUrl(String url) {
        this.url = url;
        new loadURL().execute();



        // Do this when ASYNC HAS FINISHED 
        return jObj;



    }

    class loadURL extends AsyncTask <Void, Void, Void> {

        protected void onPreExecute() {

        }

        protected Void doInBackground(Void... unused) {
            //do stuff in background 

            return (null);
        }

    }   
}

Any questions to answer just leave a comment. Thanks for helping.

like image 704
Charlton Santana Avatar asked Jun 19 '13 19:06

Charlton Santana


1 Answers

yes you use the onPostExecute method of the AsyncTask to do whatever you want to do after. That method gets called after the doInBackground is finished

like image 176
tyczj Avatar answered Oct 19 '22 19:10

tyczj