1

Scenario: you want to show a new Fragment via a click listener from within an adapter or view holder. Below are the two most common, decoupled approaches that I have seen in the past. I have noticed that they both tend to come with their own pros and cons.

Which approach do you generally prefer?

  1. Invoke a method on an interface that the Activity listens to.
  2. Publish an event that the Activity is subscribed to (event bus).
  3. Some other approach.

Please be willing to share why.

Ryan
  • 3,414
  • 2
  • 27
  • 34

3 Answers3

0

Having tried the first option and the second as well, I would go with EventBus all over again, without hesitation!

Pavlos
  • 2,183
  • 2
  • 20
  • 27
0

When a Fragment is supposed to report events to an Activity, for example to implement a two-pane layout, I use the first method because it's easy to retrieve the Activity from a Fragment and cast it to a well-defined interface. Also, when you see the Activity class declaration with the interface implementation, its role is immediately obvious. Method 2 is also valid but looks less straightforward to me.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
0

I think that for this scenario the best way to do it is first option because its implementation is easy.

emaleavil
  • 591
  • 7
  • 14