1
@Override
public void onBackPressed() {
    // If the pet hasn't changed, continue with handling back button press
    if (!mPetHasChanged) {
        super.onBackPressed();
        return;
    }

    // Otherwise if there are unsaved changes, setup a dialog to warn the user.
    // Create a click listener to handle the user confirming that changes should be discarded.
    DialogInterface.OnClickListener discardButtonClickListener =
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // User clicked "Discard" button, close the current activity.
                    finish();
                }
            };

    // Show dialog that there are unsaved changes
    showUnsavedChangesDialog(discardButtonClickListener);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // User clicked on a menu option in the app bar overflow menu
    switch (item.getItemId()) {
        // Respond to a click on the "Save" menu option
        case R.id.action_save:
            savePet();

            return true;
        // Respond to a click on the "Delete" menu option
        case R.id.action_delete:
            showDeleteConfirmationDialog();

            return true;
        // Respond to a click on the "Up" arrow button in the app bar
        case android.R.id.home: {
            // If the pet hasn't changed, continue with navigating up to parent activity
            // which is the {@link CatalogActivity}.
            if (!mPetHasChanged) {
                NavUtils.navigateUpFromSameTask(EditorActivity.this);
                return true;
            }

            // Otherwise if there are unsaved changes, setup a dialog to warn the user.
            // Create a click listener to handle the user confirming that
            // changes should be discarded.
            DialogInterface.OnClickListener discardButtonClickListener =
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            // User clicked "Discard" button, navigate to parent activity.
                            NavUtils.navigateUpFromSameTask(EditorActivity.this);
                        }
                    };

            // Show a dialog that notifies the user they have unsaved changes
            showUnsavedChangesDialog(discardButtonClickListener);
            return true;
        }
    }
    return super.onOptionsItemSelected(item);
}

I got this code from online course, and i'm confused why should we override onBackPressed()? I mean, in onOptionsItemSelected method, when the id is the same as android.R.id.home, the code is similiar to the code on onBackPressed method. Any explanation why they take this approach to show the dialogue? Can we not to override the onBackPressed method?

3 Answers 3

1

We override onBackPressed in order to customise the action the way we want when the user press on the back button on its device.

In the other hand onOptionsItemSelected invoked when the user opens the menu and press on some section in it (in your case the id for that section is "home"). and its not about the device buttons, its the menu in your app.

you can read more info about those methods:

onOptionsItemSelected
onBackPressed

0

We use onBackPressed works for device hardware back button press.

Ordinarily, the system incrementally builds the back stack as the user navigates from one activity to the next. However, when the user enters your app with a deep link that starts the activity in its own task, it's necessary for you to synthesize a new back stack because the activity is running in a new task without any back stack at all. I recommend you to read this article

0

onBackPress() is just to make sure that user is going to Close the Application.

Usually, at that point Developers promote their other Applications or Ask users to Rate the Application.

Like once User decided to Go out of Application, we can ask for confirmation as Are you sure to Exit? and we give two Options YES & NO

Not the answer you're looking for? Browse other questions tagged or ask your own question.