Not working onbackpressed when setcancelable of alertdialog is false

后端 未结 5 1136
名媛妹妹
名媛妹妹 2020-12-06 18:02

I have an AlertDialog and its setCancelable() is false. In Onbackpressed function I want the AlertDialog to be closed. But when setCancelable

相关标签:
5条回答
  • 2020-12-06 18:32

    just add a onKeyListener and cancel dialog on back key event.

        lateinit var dialog: AlertDialog
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            dialog = AlertDialog.Builder(this).setMessage("Check your internet connection")
                    .setPositiveButton("OK") { _, _ ->
                        Toast.makeText(this, "OK", Toast.LENGTH_LONG).show()
                    }.setCancelable(false)
                    .create()
            dialog.show()
    
            dialog.setOnKeyListener { _, keyCode, _ ->
                if(keyCode == KeyEvent.KEYCODE_BACK) {
                    if(dialog.isShowing) {
                        dialog.dismiss()
                    }
                }
                true
            }
        }
    
    0 讨论(0)
  • 2020-12-06 18:35

    if you don't want to close the dialog when touched outside area of the dialog, you can set the property

        permissionDialog.setCanceledOnTouchOutside(false)
    

    and if you want to dismiss the dialog on backpress you need to call the method on keyListener

     permissionDialog?.setOnKeyListener { dialog, keyCode, _ ->
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                dialog?.dismiss()
                true
            }
            false
        }
    
    0 讨论(0)
  • 2020-12-06 18:40

    The easiest work-around for this problem is to set an OnKeyListener and automatically detect when the user hits the back button.

    Java:

    public Dialog onCreateDialog(Bundle savedInstanceState) {
    
      Dialog dialog = super.onCreateDialog(savedInstanceState);
    
      dialog.setOnKeyListener(new Dialog.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
            if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_UP) {
              dialog.cancel;
              return true;
            }
            return false;
        }
      });
    
      return dialog;
    }
    

    Kotlin:

        dialog = AlertDialog.Builder(this)
                .setCancelable(false)
                .create()
        dialog.show()
    
        dialog.setOnKeyListener (object : Dialog.OnKeyListener { 
          override fun onKey(dialogInterface: DialogInterface, keyCode: Int, keyEvent: KeyEvent) {
            if(keyCode == KeyEvent.KEYCODE_BACK and keyEvent.action == KeyEvent.ACTION_UP) {
                dialog.dismiss()
                true
            }
            false
          }})
    

    Note that I added an extra condition in the if-statement, all this does is to make sure this does not fire twice.

    I hope this helps you.

    0 讨论(0)
  • 2020-12-06 18:49

    As i see you create dialogBuilder is public why not you call this in a public alertDialog and than show it using alertDilog.show() and close on back pressClick in activity and dismiss the dialog alertDilog.dismiss() override the onBackPress and dismiss it here

    val alertDialog:AlertDialog?=null
    
    
             alertDialog = new AlertDialog.Builder(this)
            //set icon 
            .setIcon(android.R.drawable.ic_dialog_alert)
            //set title
            .setTitle("Are you sure to Exit")
            //set message
            .setMessage("Exiting will call finish() method")
            //set positive button
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            //set what would happen when positive button is clicked    
                finish();
            }
            })
            //set negative button
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            //set what should happen when negative button is clicked
                Toast.makeText(getApplicationContext(),"Nothing 
              Happened",Toast.LENGTH_LONG).show();
             }
            })
            .show();
    
           onBackPress(){alertDialog.dismiss()}
    
    0 讨论(0)
  • 2020-12-06 18:56
    dialogBuilder.setOnKeyListener(object : OnKeyListener() {
                fun onKey(dialog: DialogInterface, keyCode: Int, event: KeyEvent?): Boolean {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
    //if you want to cancel the dialog only
                        dialog.cancel()
                        //if you want to finish then 
                       // finish()
                    }
                    return true
                }
            })
    
    0 讨论(0)
提交回复
热议问题