Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using event library like Otto or EventBus a recommended way to handle relations between Activities, Fragments, and background threads [closed]

In most of the case, when dealing with case

  • User thread (AsyncTask) to perform background processing
  • Pass back calculated result back to Activity or Fragment
  • Activity or Fragment re-creation might happen before user thread finishes its background processing

So far, from many reliable sources, I can see the recommended way is using Retained Fragment

Sources

  • Best practice: AsyncTask during orientation change
  • http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html
  • http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

From time to time, I heard event bus libraries is good for handling relations between Activities, Fragments, and background threads. (Please refer to https://github.com/greenrobot/EventBus. It states that performs well with Activities, Fragments, and background threads)

I came across some really popular event bus libraries

  • EventBus
  • Otto

I was wondering, when comes to handle relations between Activities, Fragments, and background threads, how is event bus approach different from Retained Fragment approach?

Which ways is a recommended way?

like image 369
Cheok Yan Cheng Avatar asked Mar 05 '15 09:03

Cheok Yan Cheng


People also ask

What is EventBus used for?

EventBus is an open-source library for Android and Java using the publisher/subscriber pattern for loose coupling. EventBus enables central communication to decoupled classes with just a few lines of code – simplifying the code, removing dependencies, and speeding up app development.

What is EventBus Java?

EventBus is a publish/subscribe event bus for Android and Java. EventBus... simplifies the communication between components. decouples event senders and receivers. performs well with Activities, Fragments, and background threads.

How do I use eventbus library?

The EventBus library is just like a radio frequencyl; if you wish to listen to any song, you simply need to set the frequency to that station. Likewise, the EventBus library posts events; if any component wants to listen to that event, then we need to register that component for the EventBus object and we will get that event in the onEvent method.

Is it easy to implement basic event bus?

As you’ll see shortly, implementing basic event bus is simple. However, despite its apparent simplicity, this component is a tricky beast to tame.

Why should I use eventbus in Salesforce?

The main reason we should use EventBus is loose coupling. Sometimes, you want to process specific events that are interested in multiple parts of your application, like the presentation layer, business layer, and data layer, so EventBus provides an easy solution for this. Simple, yet powerful. Battle-tested. High performance.

Are event buses thread-safe?

The above is the simplest implementation of an event bus which wouldn’t be practical to use in many situations (e.g. it’s not thread-safe), but it does capture the main idea. At a higher level of abstraction, event buses realize so-called Publish-Subscribe architectural pattern (“pub-sub”).


2 Answers

The event bus and Otto are not "recommended ways" by the Android developer guide primarily because they are third party libraries to simplify the task. And I believe Otto is fairly new, so older guides obviously aren't using it.

I personally like Otto, it's what I use and I haven't had any problems with it so far. But of course, that's because it suited my use-cases.

I have an example on how I used Otto here.

EDIT from the future: if you need an event bus, greenrobot/EventBus is better than Otto. Also, in some cases, LiveData<T> is perfectly sufficient instead of using event bus (which instead of emitting events to anyone, only emits to subscribers).

like image 116
EpicPandaForce Avatar answered Oct 05 '22 06:10

EpicPandaForce


I was wondering, when comes to handle relations between Activities, Fragments, and background threads, how is event bus approach different from Retained Fragment approach?

Which ways is a recommended way?

I think you misunderstand two concepts:

1) preventing a task from creating again and again when you rotate your device

2) sending messages from a thread to an activity or from a service to a fragment or...

When we put a task inside a fragment we just do not want to start it again if we are rotating. Also we want getting the result back from it for example we want to update an imageView but if you pass an imageView to an asynctask and then you rotate your device if you store the imageView as a weak reference then your imageView is null after the activity has destroyed and if you store it as a strong reference then you leak the activity. so better idea is putting it inside a fragment and storing the view as a weak reference and if the activity onCreate is called updating that reference.

EventBus and Otto are very good libraries to send messages between any components or threads. you can use those or android native solution like creating interface or localBroadcastManager or handler.

how is event bus approach different from Retained Fragment approach?

I have not looked into source code of those but I think they created a singleton queue object and store your messages inside it and dequeue it to pass your messages to their listeners.

like image 32
mmlooloo Avatar answered Oct 05 '22 05:10

mmlooloo