Long Computations in the Main Android Thread

In an Android application, you should work very hard to keep your application responsive to user actions (clicks, touches, scrolling, etc.).  That means not doing time-consuming operations like database queries or long computations in the main thread in which an activity runs.

Here are some good references for understanding this topic:

  • Painless Threading
    This is a good article on the Android Developers website. It covers the basic concepts for good user interface performance.  It is a great place to start.
  • Designing for Responsiveness
    Another article from Android Developers website. The key point for me: “any method that runs in the main thread should do as little work as possible.”
  • Intro to Loopers and Handlers
    This is an excellent explanation of Looper and Handler classes, which are classes that you use when you move tasks out of the user interface thread. The author explains a pattern that he calls the “Pipeline Thread”.

Five Simple Steps

1. Set up your activity so other threads can send messages to update user interface elements. Change the activity so it implements the android.os.Handler callback interface.

public class MyActivity extends Activity
implements Handler.Callback

2. Add an instance variable in your activity to hold a Handler. Then add the following to your activity’s onCreate method.

// Create a handler so other threads can  communicate with this one,
//  which is where UI operations take place.
mHandler = new Handler (this);

3. Implement the Handler.Callback method. This will be the place where you update views and widgets to keep the user informed of progress.

public boolean handleMessage (Message msg)

4. Take expensive operations and start them in a thread. For example, in an onClick method, add the following code:

new Thread(new Runnable() {
public void run() {
…  // something time-consuming
}  }).start();

Somewhere in the code that runs inside that thread, arrange to send a message back to the main activity. The message should convey something about the status of the ongoing operation. Exactly what that is depends on your application.

Message msg = Message.obtain (mHandler, what, arg1, arg2);
mHandler.sendMessage (msg);

5. Back in your activity, make sure that the message receiver is ready to handle the different kinds of messages that get sent. The “what” argument can be used to indicate the type of message. Something like:

public boolean handleMessage (Message msg) {
int kind = msg.what;
int arg1 = msg.arg1;
int arg2 = msg.arg2;
// update user interface views


I was surprised how easy it was to do this. A bit of research on the topic and about an hour editing and debugging was all it took.


About Bill Lahti

Bill Lahti is a software engineer building mobile applications and knowledge management solutions. Two of his interests are writing for this blog and building Android apps, with strategy games being an area of particular interest.
This entry was posted in Android and tagged , , . Bookmark the permalink.

4 Responses to Long Computations in the Main Android Thread

  1. Hi,

    Just a quick question. Can’t I use an AsyncTask (http://developer.android.com/reference/android/os/AsyncTask.html) for the same?

    Best regards, Philipp

  2. Pingback: Splash Screen Demo App for Android « More Is Not Always Better

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.