Search code examples
androidandroid-fragmentssetcontentviewandroid-connectivitymanager

How to set ContentView to Another Fragment if No internet is available and Resume Fragment if Connection is Available


After my App Login, I need to check Internet Connection before every onClick event and after every Fragment is added or replaced.If Internet Connection is not Available the setContentView should set another Fragment and as soon as the Internet get Available it should set the working fragment.
This is code for checking NetworkStatus

public class NetworkStatus {
    public static String checkConnection(Context context){

        ConnectivityManager connectivitymanager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);


        if(connectivitymanager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState()==NetworkInfo.State.CONNECTED || connectivitymanager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()==NetworkInfo.State.CONNECTED){

            return "true";

        }else{
            return "false";
        }
    }  

This is code before performing any onClick event

String NetworkStatus=NetworkStatus.checkConnection(getContext());
        if(NetworkStatus.equals("false"))
        {
            alert.noInternetAlert(getActivity());
        }
        else
        {
            performAction();
        }  

Now How to setContentView to different Fragment if no connection is available while replacing and adding new Fragments and resume to Fragment if connection get Available ?


Solution

  • Try this and see if working or not

    In you fragment in oncreate view method

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View rootView;
            if (isNetworkAvailable()) {     //if true
                rootView = inflater.inflate(R.layout.yourmainlayout, container, false);
            }
            else {      //if false
                rootView = inflater.inflate(R.layout.yourerrorlayout, container, false);
            }
    
            return rootView;
        }
    
    
    
    //for checking network i.e, isnetwork available method is
     private boolean isNetworkAvailable() {
            ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null && activeNetworkInfo.isConnected();
        }
    

    Note : For above method to work you have to add tihs two permissions in manifest file.

    <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    And if you want the user to easily fetch or relaod the activty to check network state or for reloading the activity use Swipe refresh layout.