Skip to content

Instantly share code, notes, and snippets.

@coffeearmy
Last active August 29, 2015 14:01
Show Gist options
  • Save coffeearmy/d9da34ac07f6380d433a to your computer and use it in GitHub Desktop.
Save coffeearmy/d9da34ac07f6380d433a to your computer and use it in GitHub Desktop.
OTTO in android
import com.squareup.otto.Bus;
public class OttoBusHelper {
private static Bus _currentBus;
public OttoBusHelper()
{
}
public static Bus getCurrentBus()
{
if(_currentBus==null){
_currentBus=new Bus();
}
return _currentBus;
}
}
/**
* @see http://nick.perfectedz.com/otto-event-system/
*/
public abstract class AbstractEvent
{
private Enum _type;
protected AbstractEvent(Enum type)
{
this._type = type;
}
public Enum getType()
{
return this._type;
}
}
public class OnEvent extends AbstractEvent{
private Item _Item;
private Type _type;
public enum Type
{
SAVE,DELETE
}
public OnFinishAddElectrodomesticos(Type type,Item item) {
super(type);
this._type=type;
this._item = item;
}
public Item getElectrodomestico() {
return _item;
}
}
////SENDER
OttoBusHelper.getCurrentBus().post(new OnEvent(OnEvent.Type.SAVE ,newItem));
////RECEIVER
@Override
public void onResume() {
OttoBusHelper.getCurrentBus().register(this);
super.onResume();
}
@Override
public void onPause() {
OttoBusHelper.getCurrentBus().unregister(this);
super.onPause();
}
@Subscribe
public void updateResultsInUi(OnEvent event) {
Item electItem=event.getItem();
ItemHandlerImpl.getInstance(getActivity()).insertItem(electItem);
listChanged(electItem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment