How to keep dialog open with navigation component?

Viewed 348

I'm trying to navigate from dialog to fragment with navigation-component. I'm facing the problem that dialog pops automatically after navigation because it implements androidx.navigation.FloatingWindow interface (documentation link - https://developer.android.com/guide/navigation/navigation-navigate).

So, I want to keep the underlying dialog open. What is the best option to achieve that?

My navigation graph:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/return_order_dialog_nested_nav_graph"
    app:startDestination="@id/returnOrderBottomDialog">

<dialog
    android:id="@+id/returnOrderBottomDialog"
    android:name="ru.dpd.features.returnOrder.ReturnOrderBottomDialog"
    android:label="ReturnOrderBottomDialog"
    tools:layout="@layout/dialog_bottom_return_order">
    <argument
        android:name="orderId"
        app:argType="string" />
    <action
        android:id="@+id/action_returnOrderBottomDialog_to_returnOrderDepartmentMapFragment"
        app:destination="@id/returnOrderDepartmentMapFragment" />
</dialog>

<fragment
    android:id="@+id/returnOrderDepartmentMapFragment"
    android:name="ru.dpd.features.returnOrder.tabs.map.ReturnOrderDepartmentMapFragment"
    android:label="ReturnOrderDepartmentMapFragment"
    tools:layout="@layout/fragment_departments_map">
    <argument
        android:name="orderId"
        app:argType="string" />
    <argument android:name="cityId" />
</fragment>

</navigation>

Visual graph repesentation

1 Answers

There's no official solution and it's a workaround. I've tried using a container fragment as an entry/node in the back-stack.

Actually the container launch dialog when it appears to user (in both navigate or popUp)

The container fragment doesn't have view. It launches the dialog in onCreateView. All dialog arguments (orderId,..) must be passed to the container fragment and then manually pass to dialog.

class MyDialogContainerFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        // launch the dialog
        findNavController(this).navigate(....)

        return null
    }
}
Related