5

I'm currently doing something like this in the AsyncTask's onPostExecute method, where NewTask is not the current task that's executing:

private class OlderTask extends AsyncTask<String, Void, Integer> {
    //other functions (not important)

    @Override
    protected void onPostExecute(Integer result) {
        new NewTask().execute(null, null);
    }
}

I'm wondering if this is a bad idea. Will doing so cause GC for the OlderTask to wait for the NewTask? Are there any other possible problems with using such an approach?

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

yydl
  • 24,284
  • 16
  • 65
  • 104

2 Answers2

3

Unless NewTask is inner non static class in OlderTask it will not prevent GC from collecting OlderTask unless you store reference to it in some other way.

But even if GC will wait until NewTask is done it should not be a big deal unless you save lot of data in OlderTask or create lots of copies of OlderTask.

So if your design requires doing that, it's ok. But it surely cleaner not to have chained tasks.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
  • Thanks. Would the same logic also apply if `OlderTask` is creating a new `OlderTask`? – yydl Jun 19 '11 at 06:33
  • For as long as it's different instance of `OlderTask` and it does not refer to the previous instance directly or indirectly. – Alex Gitelman Jun 19 '11 at 07:06
0

I use a callback method, So when result comes to onPostExecute I call another AsynkTask from UI, I think it is good idea, Let me know what do 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);
    }
}
Etienne Lawlor
  • 6,817
  • 18
  • 77
  • 89
Nafiz
  • 462
  • 4
  • 17