Attempting to verify exit on Android app

I already found that you cannot override the Home button on an Android phone. He quits the application, he ALWAYS quits the application, and he is NOT worried about any namby-pamby confirmation. I suppose I understand the reasoning of Google - but I find it a bit shortsighted ...

In any case (before I found out about the Home button), I set up my application so that the user can exit the application through the options menu - using onCreateOptionsMenu () and an XML file, I set up a simple menu popup that appears when the button is clicked menu. One option is Exit, and it works great.

However, it occurred to me that it might be good practice to add a confirmation dialog to the exit process (even if it could be considered redundant). So, I created an AlertDialog called "Do you want to exit?" Yes and no...

Click users for buttons are simple and just set exitConfirm (boolean) to true or false. The code that handles the selection of the Exit menu, then clears after my application and completes () or not, depending on the state of exitConfirm ...

Unfortunately, it does not work completely ... All the code in onOptionsItemSelected () is executed for the exit case, and THEN displays a dialog! I guess I should have seen that. And I believe that if I continue to fight along this path, I will figure out how to do it, but I thought that I would ask the community to offer their suggestions. Therefore, someone has a suggestion for a smooth exit from the Android application in a way that includes the step of receiving confirmation from the user

Thanks R.

+3
source share
3 answers

-, ( - ) Android. , , . , , -, .

, , , , , , , :

  • " " Android, , . onCreate(), onStart(), onResume(), onPause() onStop(). onDestroy() - onCreate() - .

  • (, "" ), , onPause() onResume(). , , , , onCreate() onDestroy().

  • , finish(), . , . , finish() - Home - "" "" .

- "", . - , - : " ?" . , , . ( ), , , "" , .

, , Google, ...

+2

- . , . . , :

.

: . . . - .

: setPositiveButton setNegativeButton .

+6

:

: Android.

: - :

protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;

        switch (id) {
        case MENU_QUIT:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(getString(R.string.main_screen_quit_text))
                    .setCancelable(false)
                    .setPositiveButton(
                            getString(R.string.main_screen_quit_yes),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {

                                    moveTaskToBack(true);
                                }
                            })
                    .setNegativeButton(getString(R.string.main_screen_quit_no),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    dialog.cancel();
                                }
                            });
            AlertDialog alert = builder.create();
            return alert;
        default:
            dialog = null;
        }
        return dialog;
    }
+4

Source: https://habr.com/ru/post/1779066/


All Articles