help

Threads -- they're your friend

Android apps handle all the UI processing on what is called either the UI or main thread -- they are the same. Because we design apps that do computationally demanding tasks (e.g., manipulate data such as traces on a map) or interact with components that introduce significant delay or latency (e.g., saves to a local database, syncing the local database with the cloud), we need to move high latency operations off the main thread -- else, the user will remove the app because it is non-responsive to UI interaction. If you don't do this then Android will inform the user via a dialogy:``Application Not Responding".

Imagine the user clicks on a button but the UI/main thread can't get to it because it's downloading a chapter of a book! That's poor design. So the user waits. Wonders what is going on with this shiny new app. Gets friustrated and utimately bins your new app you've spent a thousand hours developing.

We can fix this by shifting computationally intensive tasks or latency inducing operations off the main thread. Here's how we do that.

What this lecture will teach you

Resources

Demo projects

This code has a number of neat examples of using threads and runnables. It covers:

First, runOnUiThread: where you can run a runnable on the UI.

The second example in this demo code is the use of a runnable being posted on a view. In this example, a thread queues a runnable to the button widget which is handled the next time the button widget processes its queue. You do not have to be concerned how that works.

The final example in this code includes two helper methods that handle the callback from pressing the button. One of the methods called freeze() blocks the UI thread creating a dialog from the OS to ask if you want to kill the app. The other method spawns new threads for every click on the bottom. Comment and uncomment each method to see the behavior. Also, make sure you understand the code. That is why it's included above. Always download and run the example code.


private void runThread() {

   new Thread() {
       public void run() {
          updateButton();
          while (i++ < 1000) {
             try {
                runOnUiThread(new Runnable() {

                   @Override
                    public void run() {
                      btn_count.setText("#" + i);
                    }
                });
                Thread.sleep(300);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
            }

       private void updateButton() {
          btn_blank.post(new Runnable() {

             @Override
             public void run() {
               btn_blank.setText("posting hello from background thread"                      }
          });
       }
     }.start();
}

This code shows how a thread can implement a simple polling thread.

This code uses a handler() and messaging to send view data for the listView between the model fragment and the listView fragment -- between threads.

Notes for Threads (Important)

Important, we use the book for notes on threads and discussed above. See the chapter on Dealing with Threads.