4

I'm using sticky events in EventBus to pass my "selected" objects into the upcoming Activity. The detail activity allows the user to "select" another object to fetch a new list. I want to post another sticky event with the same object class again into yet another list activity, but from what I understand, the previous sticky will get overwritten. See the example illustration at the bottom.

What is the recommend way around this problem? I need to have n-nested sticky events with the same class in EventBus.

I really like having sticky objects, it eliminated my need to hand-wire up the Android Parcelable interface just to send objects from one living activity to another with the Intent mechanism. I'd hate to be forced to go and have to implement Parcelable even after getting a fully working EventBus just because I can't have nested sticky objects!

One way I thought up was to maintain my own stack of Item objects in an ArrayList in a Singleton, pushing when diving deeper (before startActivity) and popping when backing out (but where? onDestroy is not to be used), but the approach seems extremely fragile to me. I need a robust mechanism - this is the primary navigation pivot for my app.

  • List of items, user selects an Item. We post a sticky event with this Item and startActivity with detail view class in Intent.
  • User browses through the detail and asks for a list of related items. We start another activity showing the list.
  • User selects an Item. We post a sticky event with this second Item and startActivity with detail view class in Intent. The second Item overwrites the first Item and when the user wants to navigate up the back stack, it is no longer available to read, instead the last sticky item created is read.
Dhiraj Gupta
  • 9,704
  • 8
  • 49
  • 54

2 Answers2

1

By looking at the source https://github.com/greenrobot/EventBus/blob/master/EventBus/src/de/greenrobot/event/EventBus.java

line 52:

private final Map<Class<?>, Object> stickyEvents;

It uses the class as the key, so I don't think it can be done, but I thought of a work around.

You may do something similar to below when you set up your data:

Map<String, List> data = new HashMap<>();
data.put("CLASS_A", objectA);
data.put("CLASS_B", objectB);
EventBus.getDefault().postSticky(scheduleData);

On your receiving end:

Map<String, List> data = EventBus.getDefault().getStickyEvent(HashMap.class);
objectA = data.get("CLASS_A");
objectB = data.get("CLASS_B");

It is similar to intent, but at least there are less codes and you will get the speed gain

pjwin
  • 181
  • 2
  • 5
  • This is similar to what I ended up doing, but instead I made a `StatusEvent` class that had a map within. All events within my app get broadcasted in this same class. All interested event handlers inspect the `StatusEvent` object to determine if the event is, in fact, what the handler is waiting for and proceed accordingly. It works, but it still feels.... "klunky". – Dhiraj Gupta Jul 16 '15 at 07:30
0

I really like having sticky objects, it eliminated my need to hand-wire up the Android Parcelable interface just to send objects from one living activity to another with the Intent mechanism. I'd hate to be forced to go and have to implement Parcelable even after getting a fully working EventBus just because I can't have nested sticky objects!

The EventBus is not intended to be used in the place of Intents and bundles. You should be sending information from one activity to another using an Intent and putting the relevant parameters in the Bundle. If you are not into writing all the Parcelable and Serializable code yourself (which i can totally understand), have a look into https://projectlombok.org/ it will rock your world. You can implement parcelable and serializable with a single annotation.

shredder
  • 1,438
  • 13
  • 12