need to run tasks only after Async task has finished

15,270

Solution 1

You probably want to read more about AsyncTask on Android Developer

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

About tips, my personal choice is to pass a Boolean to onPostExecute. That way you can evaluate if the doInBackground was succesful, an then figure out what to do (Error message or update the layout).

Keep in mind that in onPostExecute method ideally should only make the screen update, assuming you have the data ok. In your example, why not include the

myJSONmap = JSONextractor.getJSONHMArrayL(jObject);

on the doInBackground? And then call

locatePlace(myJSONmap);

Like this:

class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
    String errorMsg;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Integer doInBackground(Void... v) {
        try{
            jObject = GooglePlacesStuff.getTheJSON(formatedURL);
            myJSONmap = JSONextractor.getJSONHMArrayL(jObject);
            //do stuff
            return true;
        } catch (JSONException e){
            errorMsg="Something wrong in the json";
            return false;
        }

    }

    @Override
    protected void onPostExecute(Boolean success) {
        if(success){
            locatePlace(myJSONmap);
            //update layout
        } else {
            //show error
        }


    }
}

Solution 2

You can ue below code to execute async task -

MyAsyncTask_a asyncTask_a = new MyAsyncTask_a();
                        asyncTask_a.execute();

Once doInBackground() task is finished then only control will go to postExecute(). You can't perform any UI operations in doInBackground , but you can do so in preExecute() and postExecute().

class MyAsyncTask_a extends AsyncTask<Void, Void, Integer> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Integer doInBackground(Void... arg0) {
            // TODO Auto-generated method stub

            return 1;
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);


        }
    }

Hope this will help you.

Share:
15,270
Goku
Author by

Goku

Android development, web dev, and hardware are my hobbies. I work using C# and some C++.

Updated on June 04, 2022

Comments

  • Goku
    Goku almost 2 years

    How do i make sure that the async tasks finishes before i run certain tasks. I need to use a variable AFTER the async tasks changes the value of that variable. If i run the code before async is done running then im screwed. any help? im obviously new to async tasks. If you look at my code im probably not using onPostExecute() as it was intended so advice would be helpful. My initial thought was to keep adding things to the async task but im thinking that this is just bad practice since i have tons of things that must be run in series. Basically, what i think it boils down to is: how do i make sure that the tasks in the UI thread dont start to run before my async task has finished.

    public class MainActivity extends MapActivity {
    myJSONmap;
    
    public void onCreate(Bundle savedInstanceState) {
    new AsyncStuff().execute();
    locatePlace(myJSONmap);
    
     class AsyncStuff extends AsyncTask<Void, Integer, JSONObject> {
                @Override
                protected JSONObject doInBackground(Void... params) {
                    jObject = GooglePlacesStuff.getTheJSON(formatedURL);
                    return null;
                }
                @Override
                protected void onPostExecute(JSONObject result) {
                    // TODO Auto-generated method stub
                    super.onPostExecute(result);
                    myJSONmap = JSONextractor.getJSONHMArrayL(jObject);  // getting the parsed data from the JSON object.
                    //the arraylist contains a hashmap of all the relevant data from the google website.
                }
            }
    
  • Goku
    Goku over 11 years
    Is this not what im already doing? my question is: how do i make sure that the tasks in the UI thread dont run before my async task has finished.
  • sgarman
    sgarman over 11 years
    Put the stuff you want to happen after in the onPostExecute function. IE just call locatePlace inside onPostExecute.
  • Pankaj Kushwaha
    Pankaj Kushwaha over 11 years
    put all the stuff you want to run in UI thread in post execute..return 1 makes sure that once doInBackground block is over then only it will go to post execute and run commands there
  • Goku
    Goku over 11 years
    so basically all of my remaining activity will be in the onPostExecute() method? is this good practice?
  • nsemeniuk
    nsemeniuk over 11 years
    Yes, onPostExecute() is what is needed to be done after the task has finished.
  • Goku
    Goku over 11 years
    it could be many many lines of code in the onPostExecute(). is this ok?
  • nsemeniuk
    nsemeniuk over 11 years
    Of course, imagine you need to update the whole layout, this can take several lines. Or create a dialog. You can even call another function in the activity. But remember, if you don't need to do it in the main thread, put it in the doInBackground(), that way you don't block the main activity thread. Try to leave only layouts updates on the onPostExecute() if you can.
  • Goku
    Goku over 11 years
    I think i understand now. so to sum it up simply: 1.use main thread for starting the ativity. 2.use the backgrounds for caculations and such. 3.use post execute for things that are dependent on the completion of the background thread