Intro to Android

Threading Model

UI Thread

Android is an event driven system. The main UI thread loops indefinitely waiting for a new event or user input. When something is received, it is processed.

If additional processing is required for a particular event that includes a long running operation, it should be done in another thread. Examples of long running processes are network downloads, database lookups, or file I/O.

If you attempt to do long running processing on the main thread, it's likely you will either get an exception or activity not responding dialog (ANR). This should be avoided at all costs since it's awful user experience!

Our first application doesn't do any of this sort of processing, so we will look at those topics in greater detail later.

Java Threads

You can do all the normal threading things you're used to with Java. You use Thread and Handler classes to post information back to the main calling thread.


These tasks are used to do background processing, but shouldn't be used for long running tasks. They have convenience methods that allow you to interact with the UI thread before, during, and after your process is complete. These tasks are tied to the Activity's lifecycle.


This is where you conduct long running processes that either handle networking, streaming, updating widgets, or a variety of other tasks. Services live within your Application's process boundaries.

Background Fragments

You can also use a fragment without a view as a background worker. When you inflate the view, you set the view to null. This is tied to the lifecycle of the Activity.