views:

106

answers:

2

During the most recent google io there was a presentation about implementing restful client applications. Unfortunately it was only a high level discussion with no source code of the implementation. There is one sticking point for me that I can't seem to find any information about and it's not necessary to have seen the presentation to be able to answer this question. In this diagram ( http://i.imgur.com/GlYQF.gif ) on the return path there are various different callbacks to other methods. What I don't understand is how I declare what these methods are. In other words I understand the idea of a callback (a piece of code that gets called after a certain event has happened), but I don't know how to implement it and I haven't been able to find a suitable explanation for android online yet. The only way I've implemented callbacks so far have been overriding various methods (onActivityResult for example).

I feel like I have a basic understanding of the design pattern, but I keep on getting tripped up on how to handle the return path. Thank you for any help.

+1  A: 

In many cases, you have an interface and pass along an object that implements it. Dialogs for example have the OnClickListener.

Just as a random example:

// The callback interface
interface MyCallback {
    void callbackCall();
}

// The class that takes the callback
class Worker {
   MyCallback callback;

   void onEvent() {
      callback.callbackCall();
   }
}

// Option 1:

class Callback implements MyCallback {
   void callback() {
      // callback code goes here
   }
}

worker.callback = new Callback();

// Option 2:

worker.callback = new MyCallback() {

   void callback() {
      // callback code goes here
   }
};

I probably messed up the syntax in option 2. It's early.

EboMike
A: 

Exactly what you need. I was looking for the same thing and came across this:

http://www.javaworld.com/javaworld/javatips/jw-javatip10.html

Siddharth Iyer