Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Observer pattern for notifying Fragments of a ViewPager to update their views. This will update the current Fragment, as well as the off screen Fragments that are retained.
public class FragmentObserver extends Observable {
@Override
public void notifyObservers() {
setChanged(); // Set the changed flag to true, otherwise observers won't be notified.
super.notifyObservers();
}
}
public class MyActivity extends Activity {
private MyAdapter mPagerAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.my_activity);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new MyAdapter();
pager.setAdapter(mPagerAdapter);
}
private void updateFragments() {
mPagerAdapter.updateFragments();
}
}
public class MyAdapter extends FragmentPagerAdapter {
private Observable mObservers = new FragmentObserver();
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
mObservers.deleteObservers(); // Clear existing observers.
Fragment fragment = new MyFragment();
if(fragment instanceof Observer)
mObservers.addObserver((Observer) fragment);
return fragment;
}
public void updateFragments() {
mObservers.notifyObservers();
}
}
public class MyFragment extends Fragment implements Observer {
/* Fragment related stuff... */
@Override
public void update(Observable observable, Object data) {
View root = getView();
// Update your views here.
}
}
@julyzft

This comment has been minimized.

Copy link

@julyzft julyzft commented May 26, 2016

hi.. the solution only to update the current fragment ,,how to update all fragments in adapter,,

@gokberkyagci

This comment has been minimized.

Copy link

@gokberkyagci gokberkyagci commented Jun 9, 2016

I think if u remove mObservers.deleteObservers(); line it will work all fragments @julyzft. I did not try.

@khyzhun

This comment has been minimized.

Copy link

@khyzhun khyzhun commented Jul 15, 2017

Thanks a lot!

@The-RobinHood

This comment has been minimized.

Copy link

@The-RobinHood The-RobinHood commented Dec 28, 2017

public void update(Observable observable, Object data) {

why data is always null?

@faridyagubbayli

This comment has been minimized.

Copy link

@faridyagubbayli faridyagubbayli commented Jan 30, 2018

@The-RobinHood because there are 2 types of notifyObserver:

  • notifyObservers() => data will be null
  • notifyObservers(Object data) => data will be passed to Observer's update method

Code uses first one. You can modify it to call the second one if necessary.

@TechyManik

This comment has been minimized.

Copy link

@TechyManik TechyManik commented Feb 19, 2018

public class MyFragment extends Fragment implements Observer {

/* Fragment related stuff... */

@OverRide
public void update(Observable observable, Object data) {
View root = getView();
// Update your views here.
System.out.println("Check View-->"+root);

}
}

My root is retrurning null. Kindly assist

@thalissondev

This comment has been minimized.

Copy link

@thalissondev thalissondev commented May 16, 2018

Work fine! For notify all fragments remove mObservers.deleteObservers. Work great for me.
For notify with object change your implementation of FragmentObserver:

public class FragmentObserver extends Observable {
    @Override
    public void notifyObservers(object: Object) {
        setChanged(); // Set the changed flag to true, otherwise observers won't be notified.
        super.notifyObservers(object);
    }
}
@devendroid

This comment has been minimized.

Copy link

@devendroid devendroid commented Aug 23, 2018

Not updating current visible fragment proper by passing data from activity ?
If anybody can help, Thanks.

I want to update only current visible fragment in viewpager, so I'm using
mObservers.deleteObservers(); before addObserver(-)

And sending data from activity like:

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int p =tab.getPosition();
                viewPager.setCurrentItem(p);
                myPagerAdapter.updateFragments(p);
            }

And receiving data in fragment like:

@Override
    public void update(Observable observable, Object o) {
            Integer i = (Integer) o;
            tv.setText("TAB " + i);
            Log.i(TAG, "=== " + i); 
    }

Data receiving well, But UI not updating in some cases:
case1: On the first launch first page not show updates, after swipe and come back it shows update ?
case2: On the first launch If we start page scrolling using Tab click UI not update proper, while if we start page scrolling using finger swipe it works well and after that Tab click also works well ?

@burhankhanzada

This comment has been minimized.

Copy link

@burhankhanzada burhankhanzada commented Feb 27, 2019

hello everyone who commented in this code can you tell me is this is really working or not because when i implementing this is not working

@JrajJ

This comment has been minimized.

Copy link

@JrajJ JrajJ commented Jun 11, 2019

How can we send array list through object like here
public void updateFragments() {
mObservers.notifyObservers(); // <-- here
}
and access it in MyFragment -> update(Observable observable, Object data) ??

@hackstarsj

This comment has been minimized.

Copy link

@hackstarsj hackstarsj commented Jul 17, 2019

Thanks Man! You Save my Day

@hiteshsahu

This comment has been minimized.

Copy link

@hiteshsahu hiteshsahu commented Mar 12, 2021

Work fine! For notify all fragments remove mObservers.deleteObservers. Work great for me.
For notify with object change your implementation of FragmentObserver:

public class FragmentObserver extends Observable {
    @Override
    public void notifyObservers(object: Object) {
        setChanged(); // Set the changed flag to true, otherwise observers won't be notified.
        super.notifyObservers(object);
    }
}

In Kotlin:

class FragmentObserver : Observable() {

override fun notifyObservers(arg: Any?) {
setChanged(); // Set the changed flag to true, otherwise observers won't be notified.
super.notifyObservers(arg)
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment