java - 如何在 bottomsheetdialog 显示时退出应用程序

标签 java android kotlin onbackpressed android-bottomsheetdialog

我有 bottomsheet 检查互联网是否连接!如果没有连接底片显示,如果没有底片关闭。我使用了 bottomSheetDialog.dismiss(); 函数来防止用户按下屏幕来隐藏 bottomsheet。现在我想要的是用户如果在 bottomsheet 显示退出应用程序时按下返回键

not exit bottocheet first and then exit the app

这是我目前所做的

我使用了一个名为 IOnBackPressed 的接口(interface),并且我用这段代码覆盖了“MainActivty”中的退出应用程序

 @Override
public void onBackPressed() {
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_layout);
    if (!(fragment instanceof IOnBackPressed)) {
        super.onBackPressed();
    }
}

然后我在具有底页“HomeFragment”的 fragment 中添加了退出应用程序方法

 @Override
public boolean onBackPressed() {
    bottomSheetDialog.dismiss();
    requireActivity().moveTaskToBack(true); //exit the app when press back
    requireActivity().finish();
    return true;
}

但它不起作用,当我按下返回时它不会退出应用程序。

这是我的 bottomsheetdialog 方法

    private void showBottomSheetDialog() {
    bottomSheetDialog = new BottomSheetDialog(requireContext());
    bottomSheetDialog.setContentView(R.layout.bottomsheet_no_internet);
    if (CheckNetwork.isInternetAvailable(requireActivity())) {
        bottomSheetDialog.dismiss();
    } else {
        bottomSheetDialog.setCancelable(false);
        bottomSheetDialog.show();
    }
    Button buttonNoInternet = bottomSheetDialog.findViewById(R.id.buttonNoInternet);
    assert buttonNoInternet != null;
    buttonNoInternet.setOnClickListener(v -> {
        if (CheckNetwork.isInternetAvailable(requireActivity())) {
            adapter.notifyDataSetChanged();
            bottomSheetDialog.dismiss();
        } else {
            bottomSheetDialog.dismiss();
            adapter.notifyDataSetChanged();
            bottomSheetDialog.show();
        }
    });
}

那我该怎么做呢?

最佳答案

默认情况下,在显示 BottomSheetDialog 时按下后退键时,不会调用 Activity 的 onBackPressed()。您可能可以尝试休息一下指向 onBackPressed() 内部并查看。

因此,除了 onBackPressed() 之外的解决方法是将 Key 监听器附加到 BottomSheetDialog 并检查与返回代码匹配的按键代码;在那里你可以关闭对话框并退出应用程序:

bottomSheetDialog = new BottomSheetDialog(requireContext()) {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                    // Back key is pressed

                    bottomSheetDialog.dismiss(); // Optional
                    requireActivity().moveTaskToBack(true); //exit the app when press back
                    requireActivity().finish();
                    return true;
                }
                return false;
            }
        });

        Button buttonNoInternet = findViewById(R.id.buttonNoInternet);
        buttonNoInternet.requestFocus();

    }

};

更新:

When I try this code I MUST click on tryAgain Button and then I can exit the app. But it is not exit the app when BottomSheet showen

因此,对话框显示早于 BottomSheetFragment 显示在屏幕上的原因,因此,您需要在 BottomSheetFragment 显示时显示它屏幕:

所以这部分代码需要转到BottomSheetFragment onResume()方法:

@Override
public void onResume() {
    super.onResume();
    if (CheckNetwork.isInternetAvailable(requireActivity())) {
        bottomSheetDialog.dismiss();
    } else {
        bottomSheetDialog.show();
    }
}

你可以像 BottomSheetFragmentonCreate() 方法中所说的那样设置 DialogFragment

关于java - 如何在 bottomsheetdialog 显示时退出应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68077693/

相关文章:

java - 通过 ResponseEntity 下载 XML 在 IE 中不起作用

Android刷新适配器在旋转后重新工作设备

android - 如何将 Kotlin Multiplatform Mobile 与 Amazon 放大服务集成?

Android导航 - 弹出返回堆栈时删除操作栏后退按钮

java - 通用返回

java - 如何从tomcat服务器下载文件

java - URLEncoder 中的 NullPointerException

android - Ionic 5 应用程序在启动画面后卡在白屏上

android - 设置可扩展列表的样式

Spring Boot 验证不适用于 javax.validation