1

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 ?

tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
Satyam Gondhale
  • 1,415
  • 1
  • 16
  • 43

2 Answers2

1

How about taking a layout for example :

<RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@color/white"
     android:visibility="gone">
              //Layout in case of no internet connection
  </RelativeLayout>

And then switch visibility from 'gone' to 'visible' depending upon internet connection this way you may achieve to show separate layout in same fragment life cycle.

Hope it helps.

Sahil
  • 952
  • 1
  • 7
  • 14
1

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.

krishank Tripathi
  • 616
  • 1
  • 7
  • 23