This website or its third-party tools use cookies which are necessary to its functioning and required to improve your experience. By clicking the consent button, you agree to allow the site to use, collect and/or store cookies.
Please click the consent button to view this website.
I accept
Deny cookies Go Back

Mobile app tutorials

  • Home
  • About
  • Contact
  • Login
You are here: Home / Kotlin Fragments / Kotlin sharing data between Android Fragments

November 19, 2017 by nigel

Kotlin sharing data between Android Fragments

 
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

BUY

 

Fragment Interface

Kotlin sharing data using fragment interface
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.

Fragment Arguments

Kotlin Fragment Arguments
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.

ViewModel

kotlin android fragment viewmodel
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.

Conclusion

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.

Print Friendly

Filed Under: Kotlin Fragments Tagged With: Android, Android Fragment, Android Software Development, Data Android, Kotlin, Kotlin Fragment Data Sharing, Sharing Data

  • Facebook
  • Google+
  • Twitter
  • YouTube




Recent Forum Topics

  • Kotlin Fragments
  • Kotlin Camera2 API
  • HTTP Image Libraries

Recent Posts

  • Installing and running flutter
  • Kotlin Camera2 API MediaRecorder
  • Flutter IntelliJ Tips for Beginners
  • Migrating Fragments to Jetpack Navigation
  • Flutter TabBar

Recent Posts

  • Installing and running flutter
  • Kotlin Camera2 API MediaRecorder
  • Flutter IntelliJ Tips for Beginners
  • Migrating Fragments to Jetpack Navigation
  • Flutter TabBar

Search Form

  • Facebook
  • GitHub
  • Google+
  • Twitter
  • YouTube

© 2022 - WWW.MOBAPPTUTS.COM