java - 对 OnBackPressed 感到困惑

标签 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);
}

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

最佳答案

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

另一方面,onOptionsItemSelected 在用户打开菜单并按下其中的某个部分时调用(在您的情况下,该部分的 id 是“home”)。 它与设备按钮无关,它与您应用中的菜单有关。

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

onOptionsItemSelected
onBackPressed

关于java - 对 OnBackPressed 感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51684492/

相关文章:

java - 将JSON数据解析到Android ListView中

android - 是否有适用于 Android 的 Twitter API SDK?

android - 显示摄像头 Intent 仅在Android中的Webview中?无法仅获取Camera Intent?android 5+和6+

java - 从oracle 10g列中获取小写字母

java - ffmpeg 出现空格错误

java - Dropwizard - 在服务器启动时在类中运行 init 方法

java - 如何使用curl发送参数?

android - Linkify Android 转换问题

android - 将 design.widget.BottomNavigationView 放置在相关布局的底部

java - 如何将 Java Spring logger.info 消息发送回 Angular 进行显示