Back pressed events with system alert window

后端 未结 10 1574
南笙
南笙 2021-01-03 03:21

I need to dismiss system alert window on back pressed and home button event.I have tried with onKeyEvent but in vain. As we can\'t cap

10条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 03:48

    @Override
    public void onBackPressed()
    {
           super.onBackPressed();
    }
    

    Declare this on your activity. super.OnBackPressed automatically calls back method in android. it will surely cancel your dialog.

    in addition, your dialog must look like this.

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder1.setMessage("TEST DIALOG.\n");
        builder1.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    Toast.makeText(MainActivity.this, "This Is test Dialog", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alert11 = builder1.create();
        alert11.show();
    

    or you can set Negative button..

    Hope this helps!

提交回复
热议问题