I am trying to show an alert dialog when internet goes.Everything works fine but the dialog is showing twice (overlapping one over other). why the dialog is showing twice even i am checking if the dialog instance is null before showing.Below is the code i have written.
Here is the code written in activity.
BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnected();
if (isConnected) {
internetAvailable();
} else {
showNoInternetPopup();
}
}
};
protected abstract void internetAvailable();
protected void showNoInternetPopup() {
SimpleAlertDialog alertDialog = new SimpleAlertDialog();
alertDialog.show(getSupportFragmentManager(), "1001");
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);
}
Here is the dialog class extending dialogFragment
public class SimpleAlertDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Your network seems to be unavailable")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
@Override
public void show(FragmentManager manager, String tag) {
if (manager.findFragmentByTag(tag) == null) {
super.show(manager, tag);
}
}
}
I think that the issue that u r getting a double callback form the OS. Log it to double verify. If its really the case then u need to handle the popup yourself so u wont dusplay twice, save the alert dialog as a member and then call isShown() on it. Of course chekc if its null.