Not working onbackpressed when setcancelable of alertdialog is false

后端 未结 5 1135
名媛妹妹
名媛妹妹 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: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()}
    

提交回复
热议问题