从中选择图像后无法再次打开对话框

时间:2021-01-11 19:07:09

标签: android android-intent android-architecture-navigation android-bottomsheetdialog

我有一个片段,我可以在其中打开底部对话框

private void openImageDialog() {
        NavController navController = findNavController(view);
        Timber.d("%s", navController.getCurrentDestination());
        navController.navigate(SettingUpProfileFragmentDirections.openImageDialog(resourceFile));
}

在BottomSheetDialogFragment上我有

private void openGallery() {
        Intent intent = new Intent(ACTION_PICK, EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, SELECT_IMAGE_REQUEST);
}

返回对话后,我只是像这样关闭对话

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode == SELECT_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
         dismiss();
     }
}

当我再次尝试从 Fragment 打开对话框时,出现下一个异常

java.lang.IllegalArgumentException: Navigation action/destination com.xxx:id/openImageDialog cannot be found from the current destination Destination(com.xxx:id/editProfilePhotoDialog)
        at androidx.navigation.NavController.navigate(NavController.java:938)
        at androidx.navigation.NavController.navigate(NavController.java:875)
        at androidx.navigation.NavController.navigate(NavController.java:861)
        at androidx.navigation.NavController.navigate(NavController.java:1150)
        at com.xxx.ui.fragment.SettingUpProfileFragment.openImageDialog(SettingUpProfileFragment.java:143)
        at com.xxx.ui.fragment.SettingUpProfileFragment.onProfileImageClick(SettingUpProfileFragment.java:130)
        at com.xxx.ui.fragment.SettingUpProfileFragment.lambda$onViewCreated$4$SettingUpProfileFragment(SettingUpProfileFragment.java:96)
        at com.xxx.ui.fragment.-$$Lambda$SettingUpProfileFragment$2Q5hfVos3CTEjzzPhfVwb1h-ZBs.onClick(Unknown Source:2)
        at android.view.View.performClick(View.java:7448)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Log from openImageDialog() 方法显示我仍在对话片段上 D/SettingUpProfileFragment: Destination(com.xxx:id/editProfilePhotoDialog)

我有一个想法,因为从它打开图库,我仍在对话,但不知道它如何影响导航。关于如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

dismiss() 替换 Navigation.findNavController(getActivity(), R.id.mainNavigationFragment).popBackStack() 并修复了问题。但仍然好奇为什么 dismiss() 没有按预期工作。