-3

I have bottom navigation in main Activity and have a data in MainActivity and I want to pass that to HostFragments I am using NavigationExtensions form google NavigationAndvancedSample sample here the extension function I am using in MainActivity but do you know how I can pass data which is the best way to pass data which intent contains to HostFragments? You can see I have fragmentTag and navigationHostFragment

fun BottomNavigationView.setupWithNavController(navGraphIds: List<Int>,
    fragmentManager: FragmentManager,containerId: Int,intent: Intent)



private fun obtainNavHostFragment(
    fragmentManager: FragmentManager,
    fragmentTag: String,
    navGraphId: Int,
    containerId: Int
): NavHostFragment {
  // If the Nav Host fragment exists, return it
  val existingFragment = fragmentManager.findFragmentByTag(fragmentTag) as NavHostFragment?
  existingFragment?.let { return it }

  // Otherwise, create it and return it.
  val navHostFragment = NavHostFragment.create(navGraphId)
  fragmentManager.beginTransaction()
      .add(containerId, navHostFragment, fragmentTag)
      .commitNow()
  return navHostFragment
}````





I.S
  • 1,904
  • 2
  • 25
  • 48
  • Could you describe the context on what data you want to pass from MainActivity more? Because it may be that the data flow should be redesigned so that parent activity won't communicate with fragments – bvk256 Jul 22 '19 at 13:59
  • You can use RxBus, take a look https://android.jlelse.eu/rxbus-kotlin-listen-where-ever-you-want-e6fc0760a4a8 – melianor Jul 22 '19 at 14:06
  • Why you are downvoting my question? and refereng to a doc `Also You can access activity data from fragment:` which I have downvoted for a while ago because it make tighted couple between activity and fragment and my case is not simple Activity and Fragment ,I am talking about jetpack bottomnavigation .where I am working NavHostFragment – I.S Jul 22 '19 at 15:25

1 Answers1

1

Pass data using below code

Fragment fragment = findFragmentById(R.id.activity_content_4_fregment);
if (fragment instanceof Fragment_Class) {
    ((Fragment_Class) fragment).passdata(data);
} 
Yogesh Bangar
  • 480
  • 4
  • 12