I know about SharedViewModel
(by activityviewModel
) how to share data between ViewModels.
But recently I also learned about navGraphViewModels
.
I've tried both methods in my code using Navigation Component
, but there doesn't seem to be much difference.
Both by activityViewModels
and by navGraphViewModels
confirmed that the data of screen B
is maintained even if it is returned to screen B
after moving to screen A
.
What is the difference between these two?
<?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/main_graph"
app:startDestination="@id/calendarFragment">
<fragment
android:id="@+id/calendarFragment"
android:name="com.example.lightweight.presentation.ui.calendar.CalendarFragment"
android:label="fragment_calendar"
tools:layout="@layout/fragment_calendar" />
<fragment
android:id="@+id/dailyWorkoutLogFragment"
android:name="com.example.lightweight.presentation.ui.write.DailyWorkoutLogFragment"
android:label="fragment_daily_workout_log"
tools:layout="@layout/fragment_daily_workout_log" >
<action
android:id="@+id/action_dailyWorkoutLogFragment_to_navigation"
app:destination="@id/navigation" />
</fragment>
<navigation android:id="@+id/navigation"
app:startDestination="@id/addRoutine">
<fragment
android:id="@+id/workoutListTabFragment"
android:name="com.example.lightweight.presentation.ui.write.WorkoutListTabFragment"
android:label="WorkoutListTabFragment"
tools:layout="@layout/fragment_workout_list_tab">
<action
android:id="@+id/action_workoutListTabFragment_to_writeDetailFragment"
app:destination="@id/writeDetailFragment" />
</fragment>
<fragment
android:id="@+id/writeDetailFragment"
android:name="com.example.lightweight.presentation.ui.write.WriteDetailFragment"
android:label="fragment_write_detail"
tools:layout="@layout/fragment_write_detail">
<argument
android:name="workout"
android:defaultValue="@null"
app:argType="string"
app:nullable="true" />
<action
android:id="@+id/action_writeDetailFragment_to_addRoutine"
app:destination="@id/addRoutine" />
</fragment>
<fragment
android:id="@+id/addRoutine"
android:name="com.example.lightweight.presentation.ui.write.AddRoutineFragment"
android:label="fragment_add_routine"
tools:layout="@layout/fragment_add_routine">
<action
android:id="@+id/action_addRoutine_to_workoutListTabFragment"
app:destination="@id/workoutListTabFragment" />
</fragment>
</navigation>
</navigation>