Monitoring internet connectivity in many fragments in Android

android, broadcastreceiver

Solution

You can simply apply `Observer Pattern` structure to your problem.

First of all you need to define a inner class that has to be observable by Fragments.

I will paste some code snippets to illustrate how to be applied.

Here is the `NetworkChangeReceiver` and `NetworkObservable` class.

public class NetworkChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        Log.d("NetworkChangeReceiver","Connection status changed");
        getObservable().connectionChanged();
    }

    public static class NetworkObservable extends Observable{
        private static NetworkObservable instance = null;

        private NetworkObservable() {
            // Exist to defeat instantiation.
        }

        public void connectionChanged(){
            setChanged();
            notifyObservers();
        }

        public static NetworkObservable getInstance(){
             if(instance == null){
                 instance = new NetworkObservable();
             }
             return instance;
        }
    }

    public static NetworkObservable getObservable() {
        return NetworkObservable.getInstance();
    }
}

Moreover, you need to setup your manifest for broadcast receiver. Here is how you can do it:

<receiver android:name="com.your.package.name.NetworkChangeReceiver" >
    <intent-filter>
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
       <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
</receiver>

Whenever your connection changes `onReceive` method implicitly called. Therefore, you need to call a method that notifies all the observers.

For your situation, observers will be `Fragment` classes.

I'll paste another code block to show a sample observer class.

/*
* If you have memory concerns,
* you need to add your observer while application is resuming
* you need to remove that observer object while application is pausing...
* 
*/
public class FirstFragment extends Fragment implements Observer{        
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
    } 

    @Override
    public void onPause() {
      super.onPause();
      NetworkChangeReceiver.getObservable().deleteObserver(this);
    }

    @Override
    public void onResume() {
      super.onResume();
      NetworkChangeReceiver.getObservable().addObserver(this);
    }

    @Override
    public void update(Observable observable, Object data) {
        // Whenever connection changes, this method will be called so edit your text here
    }
}

You can also pass the network status by adding any parameter to `notiftyObservers()` method.

Hope this may help.

Problem

I'm using BroadcastReceiver to monitor a possible change in the internet connectivity: ``` public class NetworkChangeReceiver extends BroadcastReceiver ``` Now, I need to make a check in each fragment in my ViewPager separately. Because if the user becomes connected then that will be reflected on the UI of each fragment separately. If I assumed that the maximim number of created fragment at a time is three, then my question is: is it dangerous to define a NetworkChangeReceiver instance for each fragment to work separately, or I should define only one?

Original source