Santosh  V M
Santosh V M

Reputation: 1561

Execute Code only after async task complete

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.

Upvotes: 0

Views: 2780

Answers (1)

shkschneider
shkschneider

Reputation: 18243

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().

Upvotes: 3

Related Questions