对OnBackPressed感到困惑

时间:2018-08-04 10:02:53

标签: java android

@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);
}

我从在线课程中获得了这段代码,我感到困惑,为什么我们应该重写onBackPressed()?我的意思是,在onOptionsItemSelected方法中,当id与android.R.id.home相同时,该代码类似于onBackPressed方法上的代码。为什么他们采用这种方法来显示对话有任何解释?我们可以不重写onBackPressed方法吗?

3 个答案:

答案 0 :(得分:1)

我们覆盖onBackPressed,以便在用户按下设备上的后退按钮时以所需方式自定义操作。

另一方面,当用户打开菜单并按菜单中的某个部分时,将调用onOptionsItemSelected(在您的情况下,该部分的ID为“ home”)。 而不是设备按钮,而是应用程序中的菜单。

您可以阅读有关这些方法的更多信息:

onOptionsItemSelected
onBackPressed

答案 1 :(得分:0)

我们将onBackPressed用于设备硬件后退按钮的按下。

通常,系统在用户从一个活动导航到下一个活动时以增量方式构建后堆栈。但是,当用户通过深链接进入您的应用程序并在其自己的任务中启动活动时,您有必要合成一个新的后堆栈,因为该活动在一个新任务中运行而根本没有任何后堆栈。我建议您阅读this article

答案 2 :(得分:0)

onBackPress()只是为了确保用户将要关闭应用程序。

通常,在那时,开发人员会推广他们的其他应用程序或要求用户对应用程序进行评级。

就像用户决定退出应用程序一样,我们可以要求您确认为您确定要退出吗?,并提供两个选项