Call notifyDataSetChanged() within Fragment

I’m using ViewPager in with five Fragments.

Now I’ve got a problem.

Running notifyDataSetChanged() works fine like this:


But how can I run notifyDataSetChanged() from a Fragment?

I have no idea.

What I’ve tried

Making ViewPager static and then run


doesn’t work.

Add a method to your MainActivity like

public void dataChanged() {

and from your fragments call it with

((MainActivity) getActivity()).dataChanged();

You can do this by various methods:

  1. The easiest one – declare a public method in as following:

    public void onDataSetChanged(){
    if(viewPager != null) {

And then call it from the fragment like this:

((MainActivity) getActivity()). onDataSetChanged();
  1. Using Interface: You can create your own interface as :

public interface OnDataSetChangeListener {
void onDataSetChanged();

Declare and define an instance of it in and then set it to the instance of your fragment. Then call it from fragment as:

  1. Using EventBuses.

And many more.