How to update fragments in the ViewPager?

author avatar

Anja Gašić

March 1, 2021

2 min read

Five indubitably easy steps

The problem described in the title below is found to be a very common problem Android developers face. As being so, I decided that is the problem I want to dedicate my first blog post to. In this quick read, I will show and explain to you a very simple solution.

When I was first encountered with this implementation, I failed to find much regarding it while searching the internet. What was offered in response either sounded like a very poor implementation or was quite complicated to deal with.

At that time I was on a project where I had to use EventBus library, and it proved to be a very good solution, with not so demanding implementation. That is why I decided to approach the solution in this way.

A sample project for this tutorial can be found on Github repository: https://github.com/Crystal-Pigeon/photo-manipulation.

To start the implementation, we need to add the following line of code to the build.gradle file:

implementation 'org.greenrobot:eventbus:3.2.0'

We start from the fact that we have one activity, in my case that was MainActivity. It contains a ViewPager with 3 tabs, which means that we have 3 fragments and a field for entering a query, which is going to be used for the search of the data from the aforementioned fragments. To display the data each of the fragments contains a RecyclerView.

Then we need to create an event that will be observed. We create a SearchEvent class that includes the following:

class SearchEvent(var query: String, var pageTitle: CharSequence?)
The next step is to create a Publisher that will broadcast the event itself.

In this case, the event is broadcasted every time something is entered in the search field. First, we create an object of class SearchEvent and broadcast the event to which we forward the created object.


et_search.doAfterTextChanged {
      val searchEvent = SearchEvent(
            et_search.text.toString(),
            searchViewPagerAdapter.getPageTitle(vp_search.currentItem)
      )
      EventBus.getDefault().post(searchEvent) //publish the event
}
Moving forward, it is necessary to register the Bus in fragments.

We will do this by overriding the onStart and onStop methods as follows:


override fun onStart() {
    super.onStart()
    if(!EventBus.getDefault().isRegistered(this)) {
        EventBus.getDefault().register(this)
    }
}

override fun onStop() {
    super.onStop()
    EventBus.getDefault().unregister(this)
}
In addition to this, it is necessary to create a Subscriber.

Specifically, we have to create a method that needs to be annotated with Subscribe, which, as a parameter takes in an object of the SearchEvent class, that we previously created. This method is executed after the broadcast of each event.


@Subscribe(threadMode = ThreadMode.MAIN)
fun onSearchEvent(searchEvent: SearchEvent) {
    // add here what needs to happen, for example display data on the screen
}

I hope you found this solution-based summary useful, and good luck with the forthcoming work.

Care to share?


How to fix missing dSYM files on Firebase Crashlytics

iOS

How to fix missing dSYM files on Firebase Crashlytics

Here is everything you can find on the topic

How to create a QR code widget

iOS

How to create a QR code widget

Yes, the long-awaited widgets for iOS devices are finally here!