How to pass data between fragments when using bottomNavView

落花浮王杯 提交于 2020-06-26 14:15:36

问题


I have an app with one activity and two fragments, And I navigate using jetpack navigation.

In my "Main" fragment, I've a button that trigger function inside the viewModel:

   edit_text_button.setOnClickListener {
            homeViewModel.onButtonClicked()
    }

onButtonClicked inside viewModel, basically shuffle the list and trigger observer in main fragment.

   fun onButtonClicked() {
        initList = (1..9).shuffled()
        _list.value = initList
    }

My question is: How can I pass every time the updated list to the second fragment in my bottom nav bar?

For example if my list is [4,5,6], I want that the list inside the other fragment will be [4,5,6] etc

UPDATE - Layout Code

MainActivity.xml

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@id/nav_view"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/mobile_navigation" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_nav_menu" />

navigation

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.example.sampleproject.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.example.sampleproject.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

</navigation>

menu items(bottom nav view)

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_dashboard" />
</menu>

MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val navView: BottomNavigationView = findViewById(R.id.nav_view)

    val navController = findNavController(R.id.nav_host_fragment)

    val appBarConfiguration = AppBarConfiguration(setOf(
            R.id.navigation_home, R.id.navigation_dashboard))
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)
}

回答1:


You can use

  • activity scope (val viewModel: YourViewModel by activityViewModels())

    or

  • navigation graph scope (val viewModel: YourViewModel by navGraphViewModels(R.id.desired_graph))

to access desired data from different fragments in that scope.




回答2:


One way is to attach DestinationChangeListener to your NavController then whenever destination changes with respect to the navController the listener will get called and you can compare destination id and pass arguments to destination.

like below

private val onDestinationChangeListener =
        NavController.OnDestinationChangedListener { controller, destination, arguments ->
           if(destination.id == <id you want to add args>){
                destination.addArgument("Mata", NavArgument.Builder().setDefaultValue(arrayOf(1,2,3,4)).build())
            }
        }

navController.addOnDestinationChangedListener(onDestinationChangeListener)


来源:https://stackoverflow.com/questions/61575958/how-to-pass-data-between-fragments-when-using-bottomnavview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!