Follow this Blog!

Nov 27, 2012

f Comment

Android How to Make Listener or Receiver Callback Methods Update UI?

Amazon First of all according to http://developer.android.com/guide/components/processes-and-threads.html:

the Andoid UI toolkit is not thread-safe. So, you must not manipulate your UI from a worker thread—you must do all manipulation to your user interface from the UI thread. Thus, there are simply two rules to Android's single thread model:

Do not block the UI thread
Do not access the Android UI toolkit from outside the UI thread

Therefore you should uphold this principle religiously. However very often when we create a listener to listen for some event how do we update the UI based on the event we've caught? Do we simply call the method on the UI object directly? Will it create a nasty exception? Is it the correct way to do things?

If you want to know how threads pass data to each other please read Android How to Make Threads Pass Data to Each Other?
Many Android developers are confused about whether an event listener runs on the main thread or a new worker thread under the hood. A general rule of thumb is that listeners run on the calling thread. So if you set the listener after creating the callback in main thread like the following example:
public class MainActivity extends FragmentActivity {
...
 public void onCreate(Bundle savedInstanceState) {
  mrtImage.setOnTouchListener(new OnTouchListener(){
   public boolean onTouch(View v, MotionEvent event) {
    ...
   }
  });
}
...
}
Then the listener runs on main thread, meaning you can manipulate your activity's UI inside onTouch() any way you want. BroadcastReceiver::onReceive() is another case in point.

Some listener allows you to set the thread on which the callback method runs. LocationManager is an example. Its requestLocationUpdates() has one variant method that accepts an argument "Looper looper" which is the target thread that runs the callback mechanism.

Some Java mechanism automatically runs on new thread, or worker thread or background thread, when it's created and triggered by the main thread. AsyncTask is an example of that.
private class FindAsyncTask extends AsyncTask<Void, String, Void> {
 @Override
 //doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing
 public Void doInBackground(Void... arg0) {
  ...
 }
}
Simply read the Java documentation of the listener or receiver class to find out which thread their callback runs on. This is important to know as it'll affect the way you update UI.

Sometimes when you manipulate UI on a thread other than the UI thread things still work fine; other times you get an ugly Java exception. Therefore be sure to do things according to Android's framework.
If you have any questions let me know and I will do my best to help you!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael