The Kotlin sharing data between fragments using ViewModel android development tutorial describes using the ViewModel to share data between Android fragments
This Kotlin on Android tutorial follows directly on from the passing data from Android activity to Android fragment tutorial.
In the Kotlin on Android ViewModel tutorial the code for sharing the data in the previous tutorial will first be removed.
A ViewModel will be created then be created with a Uri member.
If you are new to Android and/or Kotlin I recommend the Kotlin Fragments series to be done prior to this Kotlin on Android tutorial.
The key concepts this Kotlin on Android tutorial will cover are
- Creating the Android ViewModel
- Using Kotlin lazy delegate for ViewModel instance
- Setting and getting ViewModel members in Kotlin
- Calling an Android fragment from inside a fragment
In the Kotlin sharing data between fragments using ViewModel android development tutorial, we described how to use the Android ViewModel to share data between Android fragments.
Compared to the solutions from the two previous episodes, the Android ViewModel provides a much cleaner and simpler solution.
All that is required to implement the ViewModel. Then inside each fragment, implement a ViewModel instance. Which can then be called when required.