-2

Edit:

The original question was Is there a way to get a reference to a fragment that is displayed using a BottomNavigationView?. But I've figured some things out and realized I was asking the wrong question.

I'd like to get a reference to a fragment that is being displayed using a BottomNavigationView.

This is how my BottomNavigationView is being setup. It's in onCreate of an Activity.

val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.bottom_nav_view_nav_host)
    
val appBarConfiguration = AppBarConfiguration(setOf(
    R.id.navigation_first_list,
    R.id.navigation_second
))

setupActionBarWithNavController(navController, appBarConfiguration)
    bottomNav.setupWithNavController(navController)

I've tried to get the fragment with bottomNav.findFragment<TheFragmentType>() it throws an exception.

SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
  • What exception? – Selvin Dec 22 '20 at 09:09
  • IllegalStateException – SirRupertIII Dec 22 '20 at 09:24
  • @Selvin feel free to point out my idiocy. But I'd like to figure out how to get a reference to the fragment. – SirRupertIII Dec 22 '20 at 09:58
  • I just don't get it why you think that BottomNavigationView has some fragment? it is kind of menu – Selvin Dec 22 '20 at 10:00
  • I don't care if it's from the BottomNavigationView or AppBarConfiguration or navController, I just want access to the fragments displayed and the equivalent of BottomNavigationView is how it's done in iOS. I guess it's not done like this in Android. Care to point out how it is done? – SirRupertIII Dec 22 '20 at 10:05
  • I've looked through these docs: https://developer.android.com/reference/com/google/android/material/bottomnavigation/BottomNavigationView and haven't found anything promising – SirRupertIII Dec 22 '20 at 10:05
  • do you mean you wana get reference to the fragment [currently shown as content?](https://stackoverflow.com/questions/51385067/android-navigation-architecture-component-get-current-visible-fragment) – Selvin Dec 22 '20 at 10:07
  • Not necessarily, but that might work. In iOS, I can hold a reference to the "Fragment" (UIViewController) that I want, in the "Activity" then do as I need. Maybe I'm thinking of this the wrong way in Android. – SirRupertIII Dec 22 '20 at 10:16
  • What I'd really like to do is display a fragment from a push notification, but have the back stack still work correctly. I have this `MainActivity` that is shown if logged in, and that's where the app normally starts, then one of the Fragments that is represented in the bottom nav will push on another fragment with some `id` contained in the FCM push notification. – SirRupertIII Dec 22 '20 at 10:18
  • @Selvin I guess this isn't a common thing with Android? – SirRupertIII Dec 22 '20 at 18:31

1 Answers1

0

I was asking the wrong question originally. I can just use the navController to call the correct navigation component in order to show the right fragment with a back stack.

val bundle = bundleOf("someId" to "theId")
navController.navigate(R.id.action_navigation_list_to_details, bundle)
SirRupertIII
  • 12,324
  • 20
  • 72
  • 121