Issue
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
asyntask.execute();
}
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](https://popup.taboola.com/en/?templatecolorbox&utmsourceangularfix&utmmediumreferral&utmcontentthumbnails-mid:Mid Article Thumbnails:)[by Taboola](https://popup.taboola.com/en/?templatecolorbox&utmsourceangularfix&utmmediumreferral&utmcontentthumbnails-mid:Mid Article Thumbnails:)
[Sponsored Links](https://popup.taboola.com/en/?templatecolorbox&utmsourceangularfix&utmmediumreferral&utmcontentthumbnails-mid:Mid Article Thumbnails:)[Sponsored Links](https://popup.taboola.com/en/?templatecolorbox&utmsourceangularfix&utmmediumreferral&utmcontentthumbnails-mid:Mid Article Thumbnails:)
[Promoted Links](https://popup.taboola.com/en/?templatecolorbox&utmsourceangularfix&utmmediumreferral&utmcontentthumbnails-mid:Mid Article Thumbnails:)[Promoted Links](https://popup.taboola.com/en/?templatecolorbox&utmsourceangularfix&utmmediumreferral&utmcontentthumbnails-mid:Mid Article Thumbnails:)
You May Like
[
CNA
](https://www.channelnewsasia.com/singapore/tradenation-luxury-goods-scam-timeline-couple-pi-jiapeng-siriwipa-pansuk-police-interpol-2841536?cidtaboolaidpaid25042022cnamkt-news_angularfix&tblciGiCyu-3eetpfqXFavMqYA8AXMjOsh5NCt0UvXq7Kt4cjkSDsklIovYH86v6Yk5v2AQ#tblciGiCyu-3eetpfqXFavMqYA8AXMjOsh5NCt0UvXq7Kt4cjkSDsklIovYH86v6Yk5v2AQ “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
](https://f84887.bclgcl.com/?networktaboola&siteangularfix&adtitleAlalak+Selatan%3A+harga+mobil+bekas+di+tahun+2022+bisa+mengejutkan+anda&subid194593UsedCarsIDDTBTS3&subid2angularfix1489792&subid318949028_3430352808&subid4GiCyu-3eetpfqXFavMqYA8AXMjOsh5NCt0UvXq7Kt4cjkSD77lIo68nflKS8qqsR#tblciGiCyu-3eetpfqXFavMqYA8AXMjOsh5NCt0UvXq7Kt4cjkSD77lIo68nflKS8qqsR “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
](https://gain-an-intl-dubai-hotels-ace.fyi?reftaboola-angularfix&subidangularfix&subid21661-dubaihotels-taboola-all&tbid1353010&tbclicksysclick&compkeycheap+hotels+in+Dubai&rskeyDubai+Hotels+Might+Actually+Surprise+You&tblciGiCyu-3eetpfqXFavMqYA8AXMjOsh5NCt0UvXq7Kt4cjkSC1uFYopZC58I24x5OgAQ#tblciGiCyu-3eetpfqXFavMqYA8AXMjOsh5NCt0UvXq7Kt4cjkSC1uFYopZC58I24x5OgAQ “Dubai Hotels Might Actually Surprise You”)Dubai Hotels Might Actually Surprise YouDubai Hotels | Search Ads
Solution
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
@Override
protected void onPostExecute(final String result) {
// Send something back to the calling Activity like this to let it know the AsyncTask has finished.
myResponse.myResponseReceiver(result);
}
}
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
mYourAsyncTask.execute();
}
// You need to implement the function of your interface
@Override
public void myResponseReceiver(String result) {
if(!result.equals("5")) {
// You need to keep track here how many times the AsyncTask has been executed.
startAsyncTask();
}
}
}
Answered By – Reaz Murshed