Home > Enterprise >  How to navigate from BottomSheetDialogFragment to another Fragment using JetpackNavigation component
How to navigate from BottomSheetDialogFragment to another Fragment using JetpackNavigation component

Time:09-18

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)

CodePudding user response:

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)
            }

CodePudding user response:

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)
}
  • Related