Android AsyncTask Launch another AsyncTask

I am currently doing something similar in the AsyncTask onPostExecute method, where NewTask not the current task performing:

 private class OlderTask extends AsyncTask<String, Void, Integer> { //other functions (not important) @Override protected void onPostExecute(Integer result) { new NewTask().execute(null, null); } } 

I wonder if this is bad. Will the GC do this for OlderTask to wait for NewTask? Are there other possible problems using this approach?

And if this is a problem, how can I fix it?

+5
garbage-collection android android-asynctask
source share
2 answers

If NewTask is an internal non-stationary class in OlderTask , this will not prevent the GC from OlderTask unless you save the link to it in some other way.

But even if the GC waits while there is NewTask , this should not be a big problem unless you save a lot of data in OlderTask or create many copies of OlderTask .

So, if your design requires it, everything is in order. But it is certainly cleaner than chains.

+3
source share

I use the callback method, so when the result comes in onPostExecute, I call another AsynkTask interface from the user interface, I think this is a good idea. Let me know what you think.

 public class PatientSearchController extends AsyncTask < String, Void, String > { private PatientSearchResultHandler handler = null; public void onResultHandler(PatientSearchResultHandler handler) { this.handler = handler; } @Override protected String doInBackground(String...params) { } @Override protected void onPostExecute(String result) { this.handler.onResultSuccessHandler(result); } } 
0
source share

All Articles