[android-developers] Re: Use one handler for multiple activties

Listener interface example:
public interface Listener {
  public void notifyMe(Object someParam);
}
 
 
Listener example (e.g. your Activity that will implement Listener):
...
   protected void onResume() {
      ...
      backgroundThread.registerListener(this);
   }
 
   protected void onPause() {
      ...
      backgroundThread.unRegisterListener(this);
   }
 
   public void notifyMe(Object someParam) {
      // do something, like putting data from someParam into the UI.
      ...
   }
 
 
Your background thread implements these:
...
   Listener listener;
...
   public void registerListener(Listener listener) {
      this.listener = listener; // could be added to a list of Listeners as well, if necessary, for broadcasting.
   }
 
   public void unRegisterListener(Listener listener) {
      if (this.listener == listener)
         this.listener = null; // could be removed from a list of Listeners as well, if necessary, for removing from broadcasting.
   }
...
...
      // Somewhere in your background thread's code, where you need to update the UI.
      if (this.listener != null) {
         // Be sure that this code here is running on the UI thread!
         this.listener.notifyMe(someData);
      }
...
...
 
 

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en