Search code examples
androidkotlinandroid-architecture-navigation

Child DialogFragment is dismissed by parent DialogFragment dismiss()


Using Navigation Components, a Fragment opens a DialogFragment, which may open up a second DialogFragment. Dismissing the first DialogFragment, will dismiss the second as well. However, I like to let the second DialogFragment to stay active until is dismisses itself.

Probably this is as intended. In my case it's a fragment opening a custom Share Bottom Sheet dialog. The bottom sheet is dismissed after an item is tapped. When an item is clicked and the result is long lasting, a loading dialog is shown. The loading dialog dismisses itself when it is done.

So, the hierarchy is: MainFragment - Share DialogFragment - Loading DialogFragment.

I've tried navigating to the loading dialog using a global action instead of a dialog to dialog action, but that did not have any influence. Using (its' parent) MainFragmentDiretions.action** inside the Share bottom sheet resulted in a crash. Good, it should.

A solution would be:

  1. Let MainFragment open the loading dialog. I'd prefer neither DialogFragments depending on its' parent.

  2. Hide the Share bottom sheet at item click and dismiss when the loading dialog is dismissed. I also do not like this dependency.

  3. Having the lifecycle of the Loading Dialog not depend on the Share bottom sheet.

How to prevent the loading dialog from being dismissed if the share dialog is dismissed, without shifting more responsibility to MainFragment?


Solution

  • I think you're passing ChildFragmentManager of dialog fragment to your loading-DialogFragment, You can prevent the loading dialog from being dismissed by using two ways:

    1) By using android Dialog instead of DialogFragment. 2) By passing the ChildFragmentManager of MainFragment or FragmentManager of your activity, here's an example:

    class MainFragment : Fragment() {
    
        fun displayDataDialog() {
            DataDialogFrag().show(childFragmentManager, DataDialogFrag.TAG)
        }
    }
    
    class DataDialogFrag : DialogFragment() {
    
        fun displayLoadingDialog() {
            val loading = LoadingDialog()
            if (parentFragment != null) {
                loading.show(parentFragment!!.childFragmentManager, LoadingDialog.TAG)
            } else {
                loading.show(fragmentManager, LoadingDialog.TAG)
            }
        }
    
        companion object {
            const val TAG = "data_dialog"
        }
    }
    
    class LoadingDialog : DialogFragment() {
    
        companion object {
            const val TAG = "loading_dialog"
        }
    }