开发者

Android AsyncTask Starting Another AsyncTask

开发者 https://www.devze.com 2023-03-14 02:45 出处:网络
I\'m currently doing something like this in the AsyncTask\'s onPostExecute method, where NewTask is not the current task that\'s executing:

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 t开发者_C百科he 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?


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.


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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消