Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Anything similar to the iPhone SDK Delegate Callbacks?

I just switched over from iPhone to Android and am looking for something similar to where in the iPhone SDK, when a class finishes a certain task, it calls delegate methods in objects set as it's delegates.

I don't need too many details. I went through the docs and didn't find anything (the closest I got was "broadcast intents" which seem more like iOS notifications).

Even if someone can point me to the correct documentation, it would be great.

Thanks!

like image 954
Sid Avatar asked Sep 23 '10 19:09

Sid


People also ask

What is the difference delegates and callbacks iOS?

Callbacks are similar in function to the delegate pattern. They do the same thing: letting other objects know when something happened, and passing data around. What differentiates them from the delegate pattern, is that instead of passing a reference to yourself, you are passing a function.

What is Android delegate?

A delegate is just a class that provides the value for a property and handles its changes. This allows us to move, or delegate, the getter-setter logic from the property itself to a separate class, letting us reuse this logic.

What are delegate methods in iOS?

What is delegate methods in iOS? It is an easy and influential pattern in which one object in a program works on behalf of or in coordination with, another object. The delegating object keeps a reference to the other object and at the suitable time sends a message to it.

What is Apple delegate?

Overview. Your app delegate object manages your app's shared behaviors. The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system.


2 Answers

Never mind... found the answer here :)

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


Pasting from the article so as to preserve it:

Developers conversant in the event-driven programming model of MS-Windows and the X Window System are accustomed to passing function pointers that are invoked (that is, "called back") when something happens. Java's object-oriented model does not currently support method pointers, and thus seems to preclude using this comfortable mechanism. But all is not lost!

Java's support of interfaces provides a mechanism by which we can get the equivalent of callbacks. The trick is to define a simple interface that declares the method we wish to be invoked.

For example, suppose we want to be notified when an event happens. We can define an interface:

public interface InterestingEvent {     // This is just a regular method so it can return something or     // take arguments if you like.     public void interestingEvent (); } 

This gives us a grip on any objects of classes that implement the interface. So, we need not concern ourselves with any other extraneous type information. This is much nicer than hacking trampoline C functions that use the data field of widgets to hold an object pointer when using C++ code with Motif.

The class that will signal the event needs to expect objects that implement the InterestingEvent interface and then invoke the interestingEvent() method as appropriate.

public class EventNotifier {     private InterestingEvent ie;     private boolean somethingHappened;      public EventNotifier (InterestingEvent event)     {     // Save the event object for later use.     ie = event;      // Nothing to report yet.     somethingHappened = false;     }      //...       public void doWork ()     {     // Check the predicate, which is set elsewhere.     if (somethingHappened)         {         // Signal the even by invoking the interface's method.         ie.interestingEvent ();         }     //...     }      // ... } 

In that example, I used the somethingHappened predicate to track whether or not the event should be triggered. In many instances, the very fact that the method was called is enough to warrant signaling the interestingEvent().

The code that wishes to receive the event notification must implement the InterestingEvent interface and just pass a reference to itself to the event notifier.

public class CallMe implements InterestingEvent {     private EventNotifier en;      public CallMe ()     {     // Create the event notifier and pass ourself to it.     en = new EventNotifier (this);     }      // Define the actual handler for the event.     public void interestingEvent ()     {     // Wow!  Something really interesting must have occurred!     // Do something...     }      //... } 

That's all there is to it. I hope use this simple Java idiom will make your transition to Java a bit less jittery.

like image 64
Sid Avatar answered Oct 06 '22 16:10

Sid


The pendant for kotlin.

Define your interface: In my example I scan a credit card with an external library.

interface ScanIOInterface {      fun onScannedCreditCard(creditCard: CreditCard) } 

Create a class where you can register your Activity / Fragment.

class ScanIOScanner {   var scannerInterface: ScanIOInterface? = null    fun startScanningCreditCard() {       val creditCard = Library.whichScanCreditCard() //returns CreditCard model       scannerInterface?.onScannedCreditCard(creditCard)   } } 

Implement the interface in your Activity / Fragment.

class YourClassActivity extends AppCompatActivity, ScanIOInterface {     //called when credit card was scanned     override fun onScannedCreditCard(creditCard: CreditCard) {         //do stuff with the credit card information     }      //call scanIOScanner to register your interface     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {        super.onViewCreated(view, savedInstanceState)         val scanIOScanner = ScanIOScanner()        scanIOScanner.scannerInterface = this     } }  

CreditCard is a model and could be define however you like. In my case it includes brand, digits, expiry date ...

After that you can call scanIOScanner.startScanningCreditCard() wherever you like.

like image 25
kuzdu Avatar answered Oct 06 '22 14:10

kuzdu