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?