Skip to content Skip to sidebar Skip to footer

Dynamically Updating A Fragment

I basically have a MainActivity that has multiple tabs. Each tab is a ShowListFragment and it extends Fragment. Now, each tab contains data that I fetch from a database. I have a M

Solution 1:

Updated Answer:

With Android architecture components, doing this is much simpler.

The recommended pattern is using a ViewModel with LiveData members. Your fragments will register observers on the LiveData members which will automatically be coordinated with lifecycle events, e.g. unregistering in onDestroy() etc. https://developer.android.com/topic/libraries/architecture/livedata

When using the Navigation component, you can pass data when navigating to a fragment: https://developer.android.com/guide/navigation/navigation-pass-data

You can also return data from the navigated fragment: https://developer.android.com/guide/navigation/navigation-programmatic


Old answer (superseded by Architecture Components):

Since the fragments can access the activity easily enough with getActivity(), I would make the activity be the central hub for dispatching updates.

It sounds like you already have the persistence part handled with the database and all you need is some update events. So here goes:

  1. Define a listener interface. I usually do this as an inner interface within the activity:

    publicinterfaceDataUpdateListener {
             voidonDataUpdate();
         }
    
  2. Add a data structure to your activity to keep track of listeners:

    privateList<DataUpdateListener> mListeners;
    

Don't forget to initialize in the constructor:

mListeners = new ArrayList<>();
  1. Add the register/unregister methods to the activity:

    publicsynchronizedvoidregisterDataUpdateListener(DataUpdateListener listener){
             mListeners.add(listener);
         }
    
         publicsynchronizedvoidunregisterDataUpdateListener(DataUpdateListener listener){
             mListeners.remove(listener);
         }
    
  2. Add the event method to your activity:

    publicsynchronizedvoiddataUpdated(){
             for (DataUpdateListener listener : mListeners) {
                 listener.onDataUpdate();
             }
         }
    
  3. Have your fragments implement DataUpdateListener:

    publicclassMyFragmentextendsFragmentimplementsDataUpdateListener {
    

and implement the method

@OverridepublicvoidonDataUpdate() {
            // put your UI update logic here
        }
  1. Override onAttach() and onDestroy() in the fragments to register/unregister:

    @Override
         public void onAttach(Activity activity) {
             super.onAttach(activity);
             ((MainActivity) activity).registerDataUpdateListener(this);
         }
    
         @Override
         public void onDestroy() {
             super.onDestroy();
             ((MainActivity) getActivity()).unregisterDataUpdateListener(this);
         }
    
  2. Fire the event in your fragment's UI update event:

         @Override
         publicvoidonClick(DialogInterface dialog, int listIndex) {
             database.add(listIndex,object);
             database.remove(listIndex,object);
             ((MainActivity) getActivity()).dataUpdated();
         }
    

Post a Comment for "Dynamically Updating A Fragment"