Search code examples
androidandroid-recyclerviewgreenrobot-eventbus

Is EventBus good for communication from RecyclerView adapter to a fragment which is using the RecyclerView


I am using a RecyclerView to show list of videos and on clicking any video the videoplayer starts the video and player has custom controls one which is next and previous and on clicking them RecyclerView gives the next or previous video. So here I am using EventBus for this communication between adapter and video player fragment.

Its works fine but I suspect there is a leakage in EventBus communication which for some heavy application may disrupt the flow.

Is there any alternative or a fix for such communication?


Solution

  • Using EventBus for decoupling your code is good. But for you case, it's a bit overkill. Your Fragment and RecyclerView adapter are a coupled code which are directly communicate each other and inside the same Activity (I assume you're using a single Activity that host the RecyclerView and the Fragment) so you don't need to use EventBus here.

    theoretically, if you are sending a pojo (without any application context) there should'nt be any leakage.

    Instead of using EventBus, you need to use Callback / Listener. You need to create a Listener in RecyclerView adapter and set the listener inside the Activity. Whenever you're clicking the video item, tell the listener to inform the Activity. When the listener get the message, open the Fragment with the selected video.