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

4

47

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

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

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?

Soutache answered 5/3, 2015 at 9:51 Comment(0)
G
18

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).

Glossematics answered 5/3, 2015 at 9:56 Comment(8)
Are events received only when the fragment / activity is running? I mean after onResume() and before onPause()? I seem to have a case when my fragments receives a message after onPause() which is bad!Jessen
@Jessen I know that feel, which is why I have an event queue here for "post to main thread": https://mcmap.net/q/182443/-access-fragment-in-activity (note: this is NOT preserved in ANY way on process death!)Glossematics
So I understood it correctly: generally Otto does send events to paused fragments...Jessen
Well unless you block it, yes. Some people register/unregister in onResume and onPause, but the event would be lost. Which is why I personally allow sending events only if paused is false.Glossematics
I think EventBus is actually better than Otto, now that I've used both.Glossematics
@Glossematics I agree, the cashed(sticky) events are an invaluable feature.Fancier
Little update: Otto is decommissioned since august 2016 according to this announcement.Lamarlamarck
@naxa I'm talking about this Otto, although that has already been deprecated in favor of Rx, and EventBus3 is better anyways: github.com/square/ottoGlossematics
H
3

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.

Halyard answered 8/3, 2015 at 7:4 Comment(2)
By using weak reference technique, you can prevent AsyncTask from calling invalid UI, but your AsyncTask don't have the ability to interact with the re-created UI. Either retained instance fragment or event bus can overcome such shortcoming.Soutache
I would rather use asynctask with LruCache so if you call it again it just deliver the result immediately.Halyard
A
0

Communication between a simple Activity and Fragment can be easily establish in many ways but the most elegant way is by creating and using simple interface class. But for a scenario like activity that host a fragment that shows another fragments using viewpager, then this child fragment needs to communicate to the parent activity or fragment, this is where EventBus can be utilize because there will be no way you can communicate with. EventBus should only be use when there is no other way to send data to another class.

Aerolite answered 2/10, 2019 at 15:41 Comment(0)
A
0

EventBus is Best as Always.

Otto is now deprecated.

From this link of otto:

This project is deprecated in favor of RxJava and RxAndroid. These projects permit the same event-driven programming model as Otto, but they’re more capable and offer better control of threading.

If you’re looking for guidance on migrating from Otto to Rx, this post is a good start.

Anabasis answered 21/1, 2020 at 12:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.