Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Welcome, Guest. Please login or register.
September 22, 2017, 08:54:55 PM
Home Help Search Login Register
News: Please DM @puremvc on Twitter to gain forum access. Spambots are why we can't have nice things.
Pages: [1]
Print
Author Topic: Android: Events  (Read 4493 times)
mikebritton
Full Member
***
Posts: 42


View Profile Email
« on: April 05, 2011, 01:02:38 PM »



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

Code:
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:

Code:
@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):

Code:
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:

Code:
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:

Code:
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
Logged
Tekool
Sr. Member
****
Posts: 192


View Profile WWW
« Reply #1 on: April 06, 2011, 01:49:11 AM »

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.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW
« Reply #2 on: April 06, 2011, 07:27:08 AM »

@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>
Logged
Tekool
Sr. Member
****
Posts: 192


View Profile WWW
« Reply #3 on: April 06, 2011, 08:36:10 AM »

@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 and its mediator there
« Last Edit: April 06, 2011, 09:34:52 AM by Tek » Logged
mikebritton
Full Member
***
Posts: 42


View Profile Email
« Reply #4 on: April 06, 2011, 08:58:58 AM »

@tek thanks -- I like your solution better  Smiley

Logged
Pages: [1]
Print
Jump to: