0

I am trying to check for internet connection on an activity in android. I have a class `ConnectivityReceiver' which returns the current state of the network, and returns the state when a change in network is captured.

I am using the methods of this class on an activity. What I want is that whenever the method returns that there is no internet connectivity, a BottomSheetDialogFragment should show up with a 'Retry' button. On pressing the button, the Bottom Sheets dialog must close and the activity be resumed, and again there should be a check for internet again. Basically, after everytime I close the Bottom Sheets dialog, it should check for internet.

The internet connectivity class is working fine and I have checked it using logs, and it's checking everytime Network State is there. The problem is with the dialog. Everytime I close the dialog, it resumes the activity without checking for the internet.

NoInternetConnectivity.java - Class which extends 'BottomSheetDialogFragment' class.

MainActivity.java

public class MainActivity extends AppCompatActivity{
    final BottomSheetDialogFragment internetConnectivitySheet = NoInternetConnectivity
            .newInstance("New Internet Connectivity Bottom Sheet");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Manually checking internet connection
        checkConnection();
    }

    /**
     * Method to check connection on activity resume
     */
    @Override
    protected void onResume(){
        Log.d(LOG_TAG, "onResume()");
        MyApplication.getInstance().setConnectivityListener(this);
        checkConnection();
        super.onResume();
    }

    /**
     * Method to check internet connection in activity.
     */
    private void checkConnection() {
        Log.d("Check Connection called", "CHECKING CONNECTION...");
        if(!internetConnectivitySheet.isAdded() && !ConnectivityReceiver.isConnected()){
            internetConnectivitySheet.show(getSupportFragmentManager(),
                    internetConnectivitySheet.getTag());
        } else if (internetConnectivitySheet.isAdded()) {
            internetConnectivitySheet.dismiss();
        } else {
            //internet is connected :-)
        }
    }

    /**
     * Callback will be triggered when there is change in
     * network connection
     */
    @Override
    public void onNetworkConnectionChanged(boolean isConnected) {
        Log.d("On Network Change Called", "CHECKING CONNECTION...");
        if(!internetConnectivitySheet.isAdded() && !isConnected){
            internetConnectivitySheet.show(getSupportFragmentManager(),
                    internetConnectivitySheet.getTag());
        } else if (internetConnectivitySheet.isVisible()) {
            internetConnectivitySheet.dismiss();
        } else {
            //internet is connected :-)
        }
    }    
}

On checking the logs, when I close the dialog, the activity does not get resumed. What is happening, and how to fix this?

Sahil Arora
  • 875
  • 2
  • 8
  • 27
  • 1
    You activity should not be paused when adding a new fragment. Thus you should not get onResume when you remove your fragment. – Distwo Dec 15 '16 at 19:21
  • 1
    You need to detect when your bottom sheet dialog state change by setting `BottomSheetBehavior.BottomSheetCallback` and call `checkConnection()` at that time. – Distwo Dec 15 '16 at 19:26
  • @Distwo yes I am getting that. I am not getting logs that the activity resumes, so I believe the activity is not resuming, and that is why this is happening. Can you help how to do what you say? – Sahil Arora Dec 15 '16 at 19:29

1 Answers1

0

The activity will not get paused with this approach, if you want to have the flow of pause / resume on your activity you can create a custom dialog instead of the fragment, in my opinion this is a clean solution. There are a lot of posts on it How to create a Custom Dialog box in android? https://developer.android.com/guide/topics/ui/dialogs.html

However, if you want to keep the current implementation you only need to call the check after dismissing the fragment

public void onNetworkConnectionChanged(boolean isConnected) {
 //...
  } else if (internetConnectivitySheet.isVisible()) {
      internetConnectivitySheet.dismiss();
     checkConnection();
  } else {
 //....
}
Community
  • 1
  • 1
petrumo
  • 1,116
  • 9
  • 18