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.

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
Yes, I think you are right. I didn’t know about it when I wrote this note. I just learned about it the last week. It does look like a cleaner solution.
I intend to try it in the game I am working on.
AsyncTask worked out just fine. See http://blahti.wordpress.com/2011/05/18/asynctask-for-background-tasks/