1

I am trying to show dialog using navigation component in android. I have gone through the other question asked at stackoverflow Navigation Architecture Component - Dialog Fragments

I have added dialog component in nav_graph.xml as below

 <dialog
    android:id="@+id/dialog"
    android:name="com.android.example.ui.MyDialogFragment"
    tools:layout="@layout/dialog_fragment" />

I have added an action in my other fragment to show the dialog as below in nav_graph.xml

<fragment
        android:id="@+id/Fragment1"
        android:name="com.android.example.ui.Fragment1"
        android:label="Fragment"
        tools:layout="@layout/fragment1">
        <action
            android:id="@+id/action_fragment1_to_dialog"
            app:destination="@id/dialog" />
</fragment>

I have created a kotlin class MyDialogFragment as below

class MyDialogFragment : DialogFragment() {
override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(
            R.layout.dialog_fragment,
            container, false
        )
    }
}

Here from Fragment1 class, I am trying to show dialog as below

 findNavController().navigate(R.id.action_fragment1_to_dialog)

when I try to show the dialog, I get the below error

Preblem 1 is Solved

    java.lang.ClassCastException: androidx.navigation.fragment.DialogFragmentNavigator$Destination cannot be cast to androidx.navigation.fragment.FragmentNavigator$Destination
    at com.android.dairy.MainActivity$onCreate$1.onDestinationChanged(MainActivity.kt:47)
    at androidx.navigation.NavController.dispatchOnDestinationChanged(NavController.java:504)
    at androidx.navigation.NavController.navigate(NavController.java:1142)
    at androidx.navigation.NavController.navigate(NavController.java:944)
    at androidx.navigation.NavController.navigate(NavController.java:877)
    at androidx.navigation.NavController.navigate(NavController.java:863)
    at androidx.navigation.NavController.navigate(NavController.java:851)

Problem2

How can I get the instance of MyDialogFragment to update its properties or setup the button onClick listener in Fragment1

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Raj
  • 496
  • 7
  • 27
  • Please include the full stack trace for your exception. Did you read the [guide for returning a result](https://developer.android.com/guide/navigation/navigation-programmatic#returning_a_result) and specifically the [Considerations when using dialog destinations](https://developer.android.com/guide/navigation/navigation-programmatic#additional_considerations)? – ianhanniballake Feb 15 '21 at 06:07
  • my use case is different. First thing is when I try to navigate to dialog without any result then The above exception is coming. I am updating the full stack trace – Raj Feb 15 '21 at 07:04
  • The exception is coming from **your** `onDestinationChanged` callback from your `MainActivity`. Please include that code. – ianhanniballake Feb 15 '21 at 16:01
  • @ianhanniballake yes the issue was in my activity where I was casting the fragment destination. So problem 1 is solved. is there a way to get the instance of dialog fragment in the host fragment? – Raj Feb 16 '21 at 04:07
  • 1
    So...did you read the guide on returning a result? That's how you're supposed to do it. – ianhanniballake Feb 16 '21 at 04:13
  • @ianhanniballake awesome bro, its very helpful. Please post you comment as answer I will accept it. Thanks for your time – Raj Feb 16 '21 at 06:30

1 Answers1

1

Your first problem says:

java.lang.ClassCastException: androidx.navigation.fragment.DialogFragmentNavigator$Destination
    cannot be cast to androidx.navigation.fragment.FragmentNavigator$Destination
at com.android.dairy.MainActivity$onCreate$1.onDestinationChanged(MainActivity.kt:47)

So your issue is that your onDestinationChanged callback in your MainActivity is incorrectly casting your type of destination. You need to change your code to avoid that.

Your second problem is about returning a result from your dialog. The documentation covers returning a result and even has a section specifically around Additional considerations when using DialogFragments (namely, that the Fragment's Lifecycle isn't affected by a dialog, so this is one case where using the NavBackStackEntry's Lifecycle can specifically help you).

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443