0

This is my first StackOverflow question, so I apologize for any ambiguity if there's any.

I am looking for a way to receive a callback from an Android handler.

For example, if the handler is called from within the activity, what's the proper way to receive a callback from it which let's say gonna modify some text on the screen inside that activity.

Thanks! -Yevgeniy

public class MyHandler extends Handler{
  @Override
  public void handleMessage (Message msg) {
    //Do something

    // ???send back the result to the called activity???
  }
}

public class MyActivity extends Activity{

  public void callHandler(){
   MyHandler handler = new MyHandler();
   handler.sendEmptyMessage( 0 );
  }

}
ymotov
  • 1,449
  • 3
  • 17
  • 28

1 Answers1

0

At this point, from what you've shown, there isn't a lot of point in having a Handler at all. A Handler is a mechanism that is attached to the thread that creates it, soley for the purpose of scheduling tasks in that same thread, so if callHandler is called by another thread, The handler will run in whatever the thread that called callHandler was. The model I've found the most success with for Android IPC is like this

public class myActivity extends Activity {
  public Handler mHandler = new Handler()

  public void startAsynchronousTask() {
    // Fill in as necessary, or offload responsibility elsewhere
    new Thread(new Runnable() {
      public void run {
        //Do something in the background
        ...
        updateUI(results)
      }
    }).start();
  }

  public void updateUI(Results results) {
    mHandler.post(new UIUpdater(results));
  }

  private class UIUpdater implements Runnable {
    UIUpdater(Results results) {
      //construct whatever...
    }

    @Override
    public void run() {
      //Update UI
    }
  }
}

What this does, in brief, is starts an asynchronous task.... when that task is finished, it calls updateUI. updateUI puts code to be executed into the UI thread so that views can be modified, etc. In this way, as far as your asynchronous task is concerned, all it has to do is call a method to update the UI. In reality, things are a bit more complicated, but it keeps your activity's interface clean and easy to use for any asynchronous task.

JRaymond
  • 11,625
  • 5
  • 37
  • 40
  • Thanks! This is very interesting. Why do you need an updateUI to be a handler which posts another runnable? Since you're doing an asynch task to start with, maybe just update UI as part of that Runnable? – ymotov Mar 30 '12 at 18:05
  • the Idea is that your asynchronous task might not be a part of your activity class. so if in some other class you have this Activity registered as a callback, that other class could call updateUI without having to know about the handler/runnable business. If your asynchronous task is just going to run from within the Activity, then yes, you could post a UIUpdater from within the asynchronous task. The extra method is just for code clarity. – JRaymond Mar 30 '12 at 18:10
  • Cool. I think I like that. Thanks. – ymotov Mar 30 '12 at 18:29