Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialog on BackKey pressed

I have a alertDialog for my game when i press backkey of my device dialog appear. if i press "Quit" my game is close and if i press "Cancle" then toast appear for 3 seconds and after 3 seconds my game start again. Now the problem is that when first time i press backkey of my device then dialog appear and i perform some functionality its working fine but if i press backkey again without "Cancle" or "Quit" the Game then my game not resume again until i press "No" button.

The thing which i want is if i press backkey then dialog appear and if i press backkey again my game resume again without showing me dialog again thanks.

Here's my code of alert dialog:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

         //  GamePanel.thread.setStoped(true);
            GamePanel.thread.setRunning(false);

// in the next line of code we also style the dialog through xml which i put in styles


            AlertDialog alertDialog = new AlertDialog.Builder(this,R.style.myBackgroundStyle).create();
            alertDialog.setTitle("Exit Alert");
            alertDialog.setMessage("Do you really want to exit the Game?");
            alertDialog.setButton("Quit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //Best way is firstly use finish() and after that use System.exit(0) to clear static variables. It will give you some free space.
                    // A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory
                    finish();
                    System.exit(0);
                    return;

                }
            });
            alertDialog.setButton2("Cancle", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            // dialog.cancel();
                            // GamePanel.thread.resume();
                            dialog.dismiss();
                            // When user press the "Cancle" button then game resume for 3 seconds then start again
                            // Here is the Code of the toasts and each toast appear with delay of one second.

                            toast = new Toast(Game.this);
                            TextView textView=new TextView(Game.this);
                            textView.setTextColor(Color.DKGRAY);
                            textView.setBackgroundColor(Color.TRANSPARENT);
                            textView.setTextSize(60);
                            textView.setText("READY!");
                            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

                            toast.setView(textView);
                            toast.show();

                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    // show toast 2.
                                    toast = new Toast(Game.this);
                                    TextView textView = new TextView(Game.this);
                                    textView.setTextColor(Color.DKGRAY);
                                    textView.setBackgroundColor(Color.TRANSPARENT);
                                    textView.setTextSize(140);
                                    textView.setText("3");
                                    // textView.setText("done!");
                                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

                                    toast.setView(textView);
                                    toast.show();
                                }
                            }, 2500);


                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    // show toast 2.
                                    toast = new Toast(Game.this);
                                    TextView textView = new TextView(Game.this);
                                    textView.setTextColor(Color.DKGRAY);
                                    textView.setBackgroundColor(Color.TRANSPARENT);
                                    textView.setTextSize(140);
                                    textView.setText("2");
                                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

                                    toast.setView(textView);
                                    toast.show();
                                }
                            }, 5000);

                            new Handler().postDelayed(new Runnable() {
                                @Override public void run() {
                                    toast = new Toast(Game.this);
                                    TextView textView=new TextView(Game.this);
                                    textView.setTextColor(Color.DKGRAY);
                                    textView.setBackgroundColor(Color.TRANSPARENT);
                                    textView.setTextSize(140);
                                    textView.setText("1");
                                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

                                    toast.setView(textView);
                                    toast.show();

                                }
                            }, 7500);

                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run()
                                    GamePanel.thread.setRunning(true);
                                }
                            }, 10000);


                            return;
                        }
                    }

            );
            alertDialog.show();

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
like image 246
Mirza Zeeshan Avatar asked May 30 '26 04:05

Mirza Zeeshan


1 Answers

You need to set an OnKeyListener to your dialog and check whether back key is pressed.

Here is a sample code that suits your need, you need to modify your code as follows:

AlertDialog alertDialog = new AlertDialog.Builder(this,R.style.myBackgroundStyle).create();
        alertDialog.setTitle("Exit Alert");
        alertDialog.setMessage("Do you really want to exit the Game?");
        alertDialog.setButton("Quit", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //Best way is firstly use finish() and after that use System.exit(0) to clear static variables. It will give you some free space.
                // A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory
                finish();
                System.exit(0);
                return;

            }
        });


//New part regarding the back key when only dialog is shown.
alertDialog.setOnKeyListener(new Dialog.OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface arg0, int keyCode,
                KeyEvent event) {

            if (keyCode == KeyEvent.KEYCODE_BACK) {

                alertDialog.dismiss();
            }
            return true;
        }
    });

this will work only when the dialog is shown, therefore, you would be able to return to your activity

like image 175
Buddy Avatar answered May 31 '26 18:05

Buddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!