Ankit Rath
Ankit Rath

Reputation: 59

Passing data to Multiple Fragments using Navigation Component Arguments

I have a single activity which has a bottom navigation view in order to open different fragments. The top level/default fragments loads data from firebase and then i want to pass that data to different fragments when user switches to different fragment.

Navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<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/navigation"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.ankitrath.finderr.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/searchFragment"
        android:name="com.ankitrath.finderr.SearchFragment"
        android:label="fragment_search"
        tools:layout="@layout/fragment_search" />
    <fragment
        android:id="@+id/requestFragment"
        android:name="com.ankitrath.finderr.RequestFragment"
        android:label="fragment_request"
        tools:layout="@layout/fragment_request" />
    <fragment
        android:id="@+id/friendFragment"
        android:name="com.ankitrath.finderr.FriendFragment"
        android:label="fragment_friend"
        tools:layout="@layout/fragment_friendd" />
</navigation>

My MainActivity's OnCreate has:

BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
        NavController navController = Navigation.findNavController(this,  R.id.fragment);
        NavigationUI.setupWithNavController(bottomNavigationView, navController);

When HomeFragment loads the data from firestore. I want to pass 2 values to the other fragments. I did look up the docs but I couldn't understand.

Upvotes: 0

Views: 570

Answers (2)

Zea
Zea

Reputation: 155

you can use VIEWHOLDER to share data between fragments or activities, here is an example

in your app gradle add this implementation

def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

Create a viewHolder Class like that

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class SharedDataViewModel: ViewModel() {
    private val _selectedCycle = MutableLiveData<Cycle>()
    val selectedCycle: LiveData<Cycle> = _selectedCycle

    private val _isAdmin = MutableLiveData<Boolean>()
    val isAdmin: LiveData<Boolean> = _isAdmin

    fun getSelectedCycle():Cycle {
        return _selectedCycle.value!!
    }
    fun setSelectedCycle(cycle: Cycle) {
        _selectedCycle.value = cycle
    }

    fun getIsAdmin():Boolean {
        return _isAdmin.value!!
    }
    fun setIsAdmin(value: Boolean) {
        _isAdmin.value = value
    }
}

and use it like that in every fragment or activity

 private val sharedData: SharedDataViewModel by activityViewModels()

then set or get the new values

sharedData.getIsAdmin()
sharedData.setIsAdmin(true)

Upvotes: 0

Tensky
Tensky

Reputation: 99

The easiest solution for this is to just use Viewmodel instead of passing it between activity and the fragment. Then u pass the activity on the ViewModel so every fragment and the parent have the same ViewModel. This is an example of how to do it val viewModel by activityViewModels<The ViewModel that u made>()

Upvotes: 2

Related Questions