2

My app is crashing if I try to navigate to another fragment from a bottom sheet dialog fragment.

I am doing this now in bottom sheet dialog fragment:

someButton.setOnClickListener { view ->  
   val action = DestinationFragmentDirections.actionCurrentFragmentToDestinationFragment()
   view?.findNavController()?.navigate(action)
}

Crash log:

java.lang.IllegalStateException: View androidx.appcompat.widget.AppCompatButton{c44315f VFED..C.. ...P.... 44,1189-1036,1321 #7f0a00ff app:id/fragment_book_details_btn_edit} does not have a NavController set
        at androidx.navigation.Navigation.findNavController(Navigation.java:84)
        at androidx.navigation.ViewKt.findNavController(View.kt:28)
        at com.bose.bosushree.view.book_details.BookDetailsFragment.setClickListeners$lambda-4$lambda-3(BookDetailsFragment.kt:75)
        at com.bose.bosushree.view.book_details.BookDetailsFragment.lambda$OwDld1VFgq8jsjr6EbpPx5MD4aU(Unknown Source:0)
        at com.bose.bosushree.view.book_details.-$$Lambda$BookDetailsFragment$OwDld1VFgq8jsjr6EbpPx5MD4aU.onClick(Unknown Source:2)
        at android.view.View.performClick(View.java:6256)
        at android.view.View$PerformClick.run(View.java:24701)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Ankush
  • 175
  • 1
  • 12

2 Answers2

3

So, a dialog fragment works in a different window, and does not comes under the same view hierarchy of our defined navController.

In this case, we need to find our navController from navHostFragment, and then perform navigation:

someButton.setOnClickListener { view ->
                val action =
                    DestinationFragmentDirections.actionCurrentFragmentToDestinationFragment()
                val navHostFragment =
                    requireActivity().supportFragmentManager
                        .findFragmentById(R.id.nav_host_fragment_container) as NavHostFragment
                navHostFragment.navController.navigate(action)
            }
Ankush
  • 175
  • 1
  • 12
0

As per the Navigate to a Destination guide, you can use the findNavController() extension on Fragment to get the NavController. This works in any kind of Fragment including a BottomSheetDialogFragment.

someButton.setOnClickListener { view ->  
   val action = DestinationFragmentDirections.actionCurrentFragmentToDestinationFragment()
   findNavController().navigate(action)
}
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I tried that initially, but did not work for me. – Ankush Sep 13 '21 at 17:51
  • What do you mean 'did not work for me'? You _are_ adding your bottom sheet to your navigation graph via the `` tag and it by calling `navigate()`, right? – ianhanniballake Sep 13 '21 at 18:37
  • your solution worked. apology for the wrong direction. I did something wrong with view. but i tried and it worked. Thanks for this solution. But which one is better to use? My solution needs some extra line of code – Ankush Sep 14 '21 at 11:45
  • 1
    From within a Fragment, you should always be using the `Fragment.findNavController()` extension. That's why it exists. – ianhanniballake Sep 14 '21 at 17:08