PureMVC Architects Lounge

PureMVC Manifold => MultiCore Version => Topic started by: mikebritton on April 05, 2011, 01:02:38



Title: Android: Events
Post by: mikebritton on April 05, 2011, 01:02:38


When trying to figure out how to send events from Activities to Mediators, I first created interface InteractionListener for the Mediator to implement:

:
package com.britton.apps.puremvcboilerplate.view;

import com.britton.apps.puremvcboilerplate.view.event.InteractionEvent;

public interface InteractionListener {
public void interactionReceived(InteractionEvent event);
public void configureListeners(InteractionListener m);
}


Implementation in Mediator:

:
@Override
public void onRegister() {
super.onRegister();
configureListeners(this);
}

@Override
public void configureListeners(InteractionListener m) {
this.getApplication().addListener(m);
}

@Override
public void interactionReceived(InteractionEvent event) {
Log.d(TAG, "Event Received");
}


In my experimental app, Activity is subclassed as InteractionActivity (which could also be an abstract class):

:
package com.britton.apps.puremvcboilerplate.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;

public class InteractionEmitterActivity extends Activity {
public List listeners;

public InteractionEmitterActivity() {
listeners = new ArrayList();
}

public synchronized void addListener(InteractionListener il) {
        listeners.add(il);
    }

    public synchronized void removeListener(InteractionListener il) {
        listeners.remove(il);
    }
}


And in ApplicationActivity, the subclass, where there's a Button ready to trigger this miraculous handler:

:
private void initDisplay() {
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
InteractionEvent ie = new InteractionEvent(this);
Iterator it = listeners.iterator();

while (it.hasNext()) {
InteractionListener il = (InteractionListener) it.next();
il.interactionReceived(ie);
}
}
});
}


So now, any Activity that extends InteractionEmitterActivity can, by contract, send InteractionEvents to the Mediator actor.

Here's the InteractionEvent:

:
package com.britton.apps.puremvcboilerplate.view.event;

import java.util.EventObject;

public class InteractionEvent extends EventObject {

public InteractionEvent(Object source) {
super(source);
}
}

I realize this is very Flex-like; in fact, I kept thinking about Signals when I wrote it!  I've had success with them.  Maybe that's another route.


Mike


Title: Re: Android: Events
Post by: Tekool on April 06, 2011, 01:49:11
When working on my Currency app I used the standard Java java.util.Observable/java.util.Observer interfaces and made my Mediator implementing Observer. I dispatched a custom Event object. It wasn't a bad experience.


Title: Re: Android: Events
Post by: puremvc on April 06, 2011, 07:27:08
@mike It certainly looks sensible, but there's a lot of boilerplate involved that would get old after awhile.

@tek could you show us the link to your currency app code here or some snippits to illustrate your approach. I think that's going to likely be the less labor intensive route.

-=Cliff>


Title: Re: Android: Events
Post by: Tekool on April 06, 2011, 08:36:10
@puremvc Here it is: https://github.com/tekool/puremvc-android-currency-converter

There is an example of observer/Observable implementation I use in : Activity here (https://github.com/tekool/puremvc-android-currency-converter/blob/master/src/org/puremvc/java/demos/android/currencyconverter/converter/ConverterActivity.java) and its mediator there (https://github.com/tekool/puremvc-android-currency-converter/blob/master/src/org/puremvc/java/demos/android/currencyconverter/converter/view/ConverterActivityMediator.java)