0

I have this nav_graph.xml file:

<?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/nav_graph"
    app:startDestination="@id/first_fragment">

    <fragment
        android:id="@+id/first_fragment"
        android:name="FirstFragment"
        android:label="First"
        tools:layout="@layout/layout">
    </fragment>

    <fragment
        android:id="@+id/second_fragment"
        android:name="SecondFragment"
        android:label="Second"
        tools:layout="@layout/layout">

        <argument
            android:name="element"
            app:argType="Element"
            android:defaultValue="@null"
            app:nullable="true" />
    </fragment>

    <fragment
        android:id="@+id/third_fragment"
        android:name="ThirdFragment"
        android:label="Third"
        tools:layout="@layout/layout">

        <argument
            android:name="element"
            app:argType="Element"
            android:defaultValue="@null"
            app:nullable="true" />
    </fragment>
</navigation>

And I want to pass the element object from the first fragment to the last one. What I do, I add that element as an argument in each fragment. Is there any way I can define it only once, and use it in all fragments? Something like a global variable (argument)?

Jane Ashley
  • 195
  • 1
  • 1
  • 5
  • Why not search online? The solution is using ModelView: https://developer.android.com/codelabs/basic-android-kotlin-training-shared-viewmodel#0 – emandt Mar 22 '21 at 09:57
  • Access a public variable of Activity in fragment. OR Make it static in Frag1. and access it FRAG1.element – Rohaitas Tanoli Mar 22 '21 at 10:00
  • U need to add actions and use safe args https://developer.android.com/guide/navigation/navigation-pass-data#Safe-args If u didn't add default value in XML android, u need add it in Java code on actoin – Syorito Hatsuki Mar 22 '21 at 10:47

1 Answers1

0

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)
Zea
  • 145
  • 1
  • 6