Is it possible to call doInBackground from onPostExecute?


protected void onPostExecute(String s) {

I’m reading data from some API. Is it possible to call doInBackground() from onPostExecute?

I want do it recursively like (network task and update in UI ) for 5 times. Thanks in advance.

[by Taboola]( Article Thumbnails:)[by Taboola]( Article Thumbnails:)

[Sponsored Links]( Article Thumbnails:)[Sponsored Links]( Article Thumbnails:)

[Promoted Links]( Article Thumbnails:)[Promoted Links]( Article Thumbnails:)

You May Like



]( “Timeline: How a couple fled Singapore after millions were lost in Tradenation luxury goods scam”)Timeline: How a couple fled Singapore after millions were lost in Tradenation luxury goods scamCNA


Mobil Bekas | Cari Iklan

]( “Alalak Selatan: harga mobil bekas di tahun 2022 bisa mengejutkan anda”)Alalak Selatan: harga mobil bekas di tahun 2022 bisa mengejutkan andaMobil Bekas | Cari Iklan


Dubai Hotels | Search Ads

]( “Dubai Hotels Might Actually Surprise You”)Dubai Hotels Might Actually Surprise YouDubai Hotels | Search Ads


Starting the AsyncTask again from inside onPostExecute is a horrible idea. As you want to do it recursively like 5 times for network calls along with UI update, I would like to suggest you to keep an interface to keep track of the AsyncTask call.

So here’s an example about how you can achieve the behaviour. You may create an interface like this.

public interface MyResponseListener {
    void myResponseReceiver(String result);

Now you declare the interface in your AsyncTask class too. So your AsyncTask may look like this.

public class YourAsyncTask extends AsyncTask<Void, Void, String> {

    // Declare an interface 
    public MyResponseListener myResponse;

    // Now in your onPostExecute
    protected void onPostExecute(final String result) {
        // Send something back to the calling Activity like this to let it know the AsyncTask has finished. 

Now you need to implement the interface you’ve created already in your Activity like this. You need to pass the reference of the interface to the AsyncTask you’re starting from your Activity

public class MainActivity extends Activity implements MyResponseListener {
    // Your onCreate and other function goes here 

    // Declare an AsyncTask variable first
    private YourAsyncTask mYourAsyncTask;

    // Here's a function to start the AsyncTask
    private startAsyncTask(){
        mYourAsyncTask.myResponse  this;
        // Now start the AsyncTask 

    // You need to implement the function of your interface 
    public void myResponseReceiver(String result) {
        if(!result.equals("5")) {
            // You need to keep track here how many times the AsyncTask has been executed. 

Answered By – Reaz Murshed

Leave a Comment