0

In the below code snippet alertdialog.dismiss is not working don't know why. Logs work fine but the dialog does not dismiss. override fun onReceive(context: Context, arg1: Intent) {

        var builder = AlertDialog.Builder(context)
                .setTitle("Network Error !")
                .setMessage("Check your internet connection...")
                .setCancelable(false)
        var alertDialog:AlertDialog = builder.create()

            if (isConnectedOrConnecting(context)) {
                    alertDialog.dismiss()
                    Log.i("Network","Alive")

            } else{
                Log.i("Network","Dead")
                alertDialog.show()
                //alertDialog.dismiss()
            }
    }
Son Truong
  • 13,661
  • 5
  • 32
  • 58
  • does your Alert Dialog does not disappear from screen @venkatachalam S – Pie Aug 18 '18 at 12:48
  • From your logs, what's the value of "network"? Is it "Dead" or "Alive"? – Taslim Oseni Aug 18 '18 at 12:56
  • @Taslim i wrote the code inside my broadcast receiver. based on my netwrk connection i'm getting network value as alive and dead. but alert dialog not getting dismiss – venkatachalam S Aug 18 '18 at 13:28
  • @Pie yes. my Alert Dialog not getting dismissed but the log of value alive is getting without any problem – venkatachalam S Aug 18 '18 at 13:30
  • @venkatachalam Did you see my answer. – Pie Aug 18 '18 at 13:32
  • I think I can see the problem now.. kindly update the question with a screenshot of your log. In your log, just search for "Network" (for simplicity) and take a screenshot or just paste the logs.. Any way that's convenient. – Taslim Oseni Aug 18 '18 at 14:37

2 Answers2

1

Problem Solved.

Initialize the builder.create in the place where we call alert.show

var alertDialog:AlertDialog? = null
override fun onReceive(context: Context, arg1: Intent) {
    var dialogBuilder = AlertDialog.Builder(context).setTitle("Network Error !")
            .setCancelable(false)
            .setMessage("Check your internet connection...")

    if (isConnectedOrConnecting(context)) {
        //initializeDialog(context)
        alertDialog!!.dismiss()
        Log.i("Network","Alive")
    }else{
        alertDialog = dialogBuilder.create()
        alertDialog!!.show()
        Log.i("Network","Dead")
        //initializeDialog(context).create()
    }
}
Community
  • 1
  • 1
0

You should be able to use either Dialog.dismiss(), or Dialog.cancel()

alertDialog.setNeutralButton("OK", new DialogInterface.OnClickListener() { // define the 'Cancel' button
    public void onClick(DialogInterface dialog, int which) {
        //Either of the following two lines should work.
        dialog.cancel();
        //dialog.dismiss();
    } 
});

Otherwise you can dismiss your dialog after few seconds

final AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?");
dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int whichButton) {
        exitLauncher();
    }
});     
final AlertDialog alert = dialog.create();
alert.show();

// Hide after some seconds
final Handler handler  = new Handler();
final Runnable runnable = new Runnable() {
    @Override
    public void run() {
        if (alert.isShowing()) {
            alert.dismiss();
        }
    }
};

alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        handler.removeCallbacks(runnable);
    }
});

handler.postDelayed(runnable, 10000)
Pie
  • 584
  • 1
  • 6
  • 22
  • i dont want a button. I need a alert dialog without a button. when internet connection is not available dialog is showing but when netwok connection is available i want to dismiss the alert dialog. – venkatachalam S Aug 18 '18 at 13:32
  • @venkatachalam you can set set ok button in builder if internet is not available.User taps on it and it will disappear from screen – Pie Aug 18 '18 at 13:57
  • did you see my answer?. I have updated my answer. @venkatachalam – Pie Aug 18 '18 at 14:11