AsyncTask implementation framework – to be independent from Activity –

Continuing from AsyncTask usage summary. Interest is how to establish independence between Activity and AsyncTask? The answer is well summarized in the article, Android AsyncTask はバックグラウンドの処理に集中すべし!”. I will just translate and re-summarize this article. 3 ways will be introduced. 

Advance – implementation framework of AsyncTask

A. Define as inner class of Activity

Most intuitive, easy and faster way is to implement your AsyncTask inside Activity which use it. It is nice choice when CustomAsyncTask is specific (highly dependent on) to the parent Activity.

Mock implementation is like below, here text of mEditText is updated after AsyncTask’s background process.

The problem is that member of Activity, mEditText, is used inside onPostExecute() method of CustomAsyncTask. So this is basically only applicable for this Activity. To separate the work of common AsyncTask part and Activity’s UI update part, we can take following methods. One way is to override methods at Activity, the other way is to implement custom callback.

B. Override UI thread task at Activity side

Consider like this. AsyncTask should concentrate on background task, Activity side will do all UI update task. We can achieve it by overriding UI thread task (onPreExecute, onProgressUpdate and onPostExecute) at Activity. 

Mock implementation 

As you can see, CustomAsyncTask only implements doInBackground() method. All other UI update method is implemented in Activity (caller) side. It is a tricky implementation, but it enables us to divide the role of AsyncTask and Activity. 

One concern is it has less encapsulation (basically, Activity side can do any UI update based on the result). No common behavior of onPostExecute() is implemented in AsyncTask side, This implementaton may differ among Activities.


C. Register callback to invoke Activity method

The last, most organized way to make AsyncTask independent from Activity is to implement callback. We can reduce dependency between Activity and AsyncTask, but you need to take care about designing callback method types. 

Callback methods are implemented as interface in below mock implementation.

To use this callbacks at Activity side, Activity can implement this callback interface. Then, we can implement the function of this callback at Activity side to update UI.  

Summary table

  A. Inner class implementation B. Override UI thread methods C. Callback implementation
Independent from Activity?

  • Easy
  • Fast
  • Faster
  • Flexible
  • Most organized
  • The AsyncTask is only for parent Activity
  • Tricky 
  • Haphazard 
  • Strict design is needed
  • Troublesome

Conclusion – which way to use?

  • Implementation speed is more important. (First implementation, debugging etc)
  • It is ok that the CustomAsyncTask is only for specific Activity
    → A. Implement as inner class of Activity

  • Personal, small team project.
  • Faster, more flexible, unplanned implementation.
    → B. Override UI thread task from Activity side

  • Official, team project.
  • Specification is designed concretely.
    → C. Use callback at Activity side

* This is just a personal opinion. Please find yourself which method to adopt for your project.


Sponsored Links

Leave a Reply

Your email address will not be published.