Supercharge your Android Application-4(Threads).

Posted on Feb 5, 2015 , 3 minute read

Table Of Contents

In this series so far we had,

  • part-1 on how to take care of loading our list.
  • part-2 on how to load our UIs faster
  • part-3 on how to take care of downloading and saving our data

As said in last part, this can be the shortest post of the series. Because many of us are already using one or other kind of libraries to work with networks and don’t have to do any manual thread handling code. In that case you can miss next tip which is,

7. AsyncTask works* *conditions apply

We are taught to use AsyncTask whenever we need to do some lengthy operations for UI thread. We are told it will work, and we have found it true many times, especially in the era were activities were everything. But now times are changed, we do have fragments which can be resumed and paused on demand. Even activities are no longer guaranteed to be always resumed. And at the best, our AsyncTask needs to work with UI thread in onPreExecute and onPostExecute methods, where it will crash when hosting activity or fragment is no longer visible or lost context.

In most of the times we need this async task to download the data (and store it in database or), directly show the result. Most of the times we don’t have to show exact progress (like 67 of 300 kittens downloaded). In such cases, you can always use IntentService to download the data (and store it if needed). Unlike traditional services IntentService run purely in background, and you don’t have to worry about terminating it, it will automatically terminates itself when execution of onHandleIntent() is completed. While it’s already running, IntentService will not throw an exception or cancel the current operation if you try to start the service again.1 2

At the end when you have got all the data, you may want to notify the UI about the operation, which can be easily achieved by using LocalBroadcastManager. This is very useful component to broadcast our operations locally within our application, and it’s guaranteed that such broadcasts will never be trapped by other process, thus the data generated by these broadcasts will never go away.

8. One drawable to rule them all.

Asset tinting is one of the most useful concept to reduce APK size and reduce drawable’s loading time. I was introduced to this subject by Vinay Gaba in GDG Ahmedabad’s devfest, in his slides which you can see here. This post from Dan Lew covers asset tinting from coding perspective. But I have one thing to add in all this tinting thing.

If you tint your resource drawable, all instances of the drawables will be tinted. This may be problemetic if you want to tint only one instance of the drawable. In that case call drawable.mutate() before applying your tinting and only that drawable will be tinted.3

This is it. The series for Supercharging your applications rests for now. As the platform evolves and so it’s problems, I am sure it will resurrect soon, and we will have new tips and tricks in our sleeves. Till then keep enjoying your development with less bugs.

  1. GCM always use IntentService to read data about notifications ↩︎

  2. Loader is also an option, but it’s bit complecated to implement and the easiest is based on AsyncTask. So I do not prefer loaders. But if you do, you can read Alex Lockwood’s blog. There are many posts in that blog about thread, loaders and lifecycle management. ↩︎

  3. In comments of dan lew’s post the first comment already mentions this topic, Romain Guy has written a post on it, also reposted it in Android Developers blogpost ↩︎

See Also



- Network      - API Calls      - Drawable