The Kotlin sharing data between Android Fragments introduction describes the various methods of sharing data Android Fragments using the Kotlin language.
This Kotlin on Android tutorial series follows on from the Kotlin fragments series. Which explains the different methods for switching between Android Fragments.
This series will focus on sharing an Android Uri between two different Android fragments.
The first Android fragment will consist of two buttons. One for recording and the other for video playback.
The second Android fragment will have a VideoView where video playback will take place.
The first two Kotlin on Android episodes describe the traditional Android methods for sharing data across Android fragments. Which also requires the support of the calling activity.
The last Kotlin on Android episode uses the new ViewModel for sharing data between two Android fragments. And can be used without the support of the calling activity.
Each episode will be released on a weekly basis. Though there is the option of getting access to all the episodes straight away by buying the 5 episode series for $10.00
This Kotlin on Android tutorial will describe how to share data from an Android fragment to an Activity.
An interface needs to be created inside the fragment. Fortunately, with Android Studio a basic fragment can be created, which includes a basic implementation of an interface and listener.
An Android intent will be used to start a video application. The video application will return a Uri, which will be passed to the calling Android activity.
This Kotlin on Android tutorial will describe how to create an Android fragment with Android studio. And implement the fragment arguments member to pass the Uri during fragment creation.
An Android VideoView will be created inside the fragment. Which will use the Uri for starting video playback.
This Kotlin on Android tutorial will use the ViewModel for sharing data between fragments.
The fragment interface and arguments code from the previous Kotlin on Android tutorials will be removed.
Then an Android ViewModel class created. The two Android fragments will create an instance of the ViewModel. The VideoIntentFragment will set the Uri value in the ViewModel. The VideoViewFragment will get the Uri value from the ViewModel.
The first two Kotlin on Android tutorials required a considerable amount of code to support passing the Uri data value from the VideoIntentFragment to the VideoViewFragment.
Also, the calling Android activity was required to implement the fragment interface in order to received data from the Android fragment.
The last Kotlin on Android episode used the new Android ViewModel. The ViewModel did not require any Android activity code. And it’s implementation was straightforward and consistent between the two Android fragments.