Handle Android AsyncTask Configuration Change Using Fragment

AsyncTask is much preferred way for background processing of some task with easy use of UI thread. It powers you to peform background work and publish results to UI thread without manipulating Threads or Handlers. General usage of AsyncTask includes performing short network or intensive data operations.


We can use IntentService in conjunction with ResultReceiver also for acheiving same result. If interested, you can check our detailed tutorial. Android IntentService: Working and Advantages

With these bundled benefits of AsyncTask, it provides hard time to handle it over configuration change. During configuration change if not handled, it may lead to following problems.

1. Memory Leak : Problem with AsyncTask is that it has an implicit reference of enclosing activity. In case of configuration change if not handled properly, enclosing activity instance will be destroyed but it will not be garbage collected until AsyncTask is running. In case of multiple async tasks running, it may lead to memory leakage. While using AsyncTask you should try that enclosing destroyed activity is freed from memory.

2. Lost Progress and Result: If AsyncTask is used with intention to publish progress and results to Activity UI, they will be discarded as previous activity instance is destroyed. It’s good practice to utilize previous results and progress to current enclosing instance of activity UI update rather than performing same task again.

We will encounter above two problem using Headless Fragments. Fragment which do not have UI are referred as Headless Fragment as they are not part of user interaction. Headless fragments are meant for data encapsulation and can be used for data processing sharing. We will make most out of  fragment’s retaining property which can be set using Fragment.setRetainInstance(boolean). If true, fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be used with fragments not in the back stack.

You can check out our tutorial on Fragments for their detailed benefits and usage.
Android Fragments: Building dynamic UI, Why to use Fragments?

Sample Android Application

 

[su_button url=”https://github.com/androidsrc/HeadlessFragment” target=”blank” style=”stroked” background=”#51d461" color=”#ffffff” size=”6" center=”yes” radius=”0" icon=”icon: arrow-circle-o-down”]Download Complete Source Code[/su_b[/su_button]p> 

Headless_Fragment_Hanlde_Orientation

 

1. Create new android application project with package name “com.androidsrc.headlessfragment” and application name as “HeadlessFragment”.

2. Preparing Application Manifest File

We will have only one activity in our application for Handling Android AsyncTask Configuration Change. No permission is required to mention. Final AndroidManifest.xml will be as below.

You may also like...

4 Responses

  1. July 10, 2015

    […] using single layout only. If that is the case, then this is best solution. But, if not then try this, which basically is Headless Fragment as pointed by […]

  2. September 9, 2015

    […] I have been trying to develop an in app billing feature in my app and even though using headless fragment to actual do the in app billing, makes more sense after seeing this – http://cs2guru.com/handle-android-asynctask-configuration-change-using-fragment/ […]

  3. September 9, 2015

    […] I have been trying to develop an in app billing feature in my app and even though using headless fragment to actual do the in app billing, makes more sense after seeing this – http://cs2guru.com/handle-android-asynctask-configuration-change-using-fragment/ […]

Leave a Reply

Your email address will not be published. Required fields are marked *