0

I have the following three lines of code.

Line 1 : GetBitMapFromURL gbmap = new GetBitMapFromURL();   //Obtain thumbnail bitmap
Line 2 : gbmap.execute(applicationThumbNailURL);
Line 3 : applicationThumbnailBitMap = gbmap.returnBitmap();

I want Line 3 to be executed only after GetBitMapFromURL async task's onPostExecute is executed.

Santosh V M
  • 1,541
  • 7
  • 25
  • 41
  • 3
    Please explain why. Why can't you put it into the onPostExecute callback? – Simon Oct 31 '12 at 16:39
  • @Simon I'm using the same async task to perform different tasks. – Santosh V M Oct 31 '12 at 16:43
  • 1
    http://stackoverflow.com/questions/10048958/android-calling-asynctask-right-after-an-another-finished and http://stackoverflow.com/questions/7494515/android-can-i-chain-async-task-sequentially-starting-one-after-the-previous-as – MKB Oct 31 '12 at 16:46
  • Why not pass a parameter into the `AsyncTask` constructor that determines the type of task it will perform? You can then check for the value of the parameter in `onPostExecute` and conditionally perform the assignment of the bitmap. – dave.c Oct 31 '12 at 17:52

1 Answers1

3

Create a callback in GetBitMapFromURL.

public class GetBitMapFromURL extends AsyncTask<Void, Void, Void> {

    private GetBitMapFromURLCallback mCallback = null;

    public WebService(GetBitMapFromURLCallback callback) {
        mCallback = callback;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        // ...
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);

        if (mCallback != null) {
            mCallback.onGetBitMapFromURLComplete(this);
        }
    }

    public interface GetBitMapFromURLCallback {

        public void onGetBitMapFromURLComplete(GetBitMapFromURL getBitMapFromUrl);
    }
}

public class MyActivity extends Activity implements GetBitMapFromURLCallback {

    // ...

    public void onGetBitMapFromURLComplete(GetBitMapFromURL getBitMapFromUrl) {
        // This code will get called the moment the AsyncTask finishes
    }
}

And let your activity implements this callback and onGetBitMapFromURLComplete().

shkschneider
  • 17,833
  • 13
  • 59
  • 112