Share Data Between Fragments Used ViewModel Example

It’s very common in the Android application that two or more Fragments in an Activity need to communicate with each other. Let see a common case of master-detail fragments, where you have a fragment in which the user selects an item from a list and another fragment that displays the contents of the selected item. In this tutorial, we are using a ViewModel class as an interface between Fragments to sharing data.

Share data between fragments used ViewModel examples


What are Fragments :

Android Fragment represents a behavior or a portion of user interface in an Activity (or in FragmentActivity). You can use multiple fragments in a single activity to build a multi-pane UI and can reuse a fragment in multiple activities.


ViewModel :

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. Here we are using ViewModel class as an interface between Fragments to sharing data.

Consider we have 2 fragments Sender and Receiver and Suppose you need to share data between Sender and Receiver.

For this, you have to create a ViewModel class and implements methods in Sender pass the data to the Receiver, where receiver have an observer to see change and update UI accordingly.


Let’s start building basic: How to share data Between two fragments

Step 1. Create an android project in the android studio (Android First Program in Android Studio)
Step 2. Create 2 fragments in activity_main.xml

add following code :

Step 3. Create new class SharedViewModel and extend View Model

Initialize variable “message” for sharing data and Generate Getter and setter.

Step 4. Create a layout and activity file for sender fragment with a button to send a message

Copy paste code must add color code in res/values/colors.xml before its throw error

Then sender layout file

Create an activity class for Sender

Note: when you are creating sender file that time also can generate layout file for the same fragment.

Create an instance of SharedViewModel model and button.setOnClickListener to send data

Step 6. Create a layout and activity  file for the receiver

Add TextView in Fragments to show shared data

Create an activity class for Receiver

get the result in ViewModel object observer and show in TextView.

Output Screenshot: Before

Pass data between fragments used ViewModel example

After click button “SEND HELLO”

How to share data between fragments used ViewModel example


Download Link and Source code Share data between fragments used ViewModel example

Code in Kotin


Note : This example (Project) is developed in Android Studio 3.0.1 ,tested on Android 7.1.1 ( Android Nougat), compile SDK version API 26: Android 8.0 (Oreo)



Coding in JAVA

Bonus: For share data between fragment using interface check this tutorial: Basic Communication between two fragments (Pass data)


4 thoughts on “Share Data Between Fragments Used ViewModel Example

  1. Hello, can you help me achieve the same result in Kotlin?
    Specifically on the part of the receiver, because I think I’ve got everything else already.

    1. Check tutoiral, we updated kotlin source code…
      class ReceiverFragment : Fragment() {

      internal lateinit var tv_msg: TextView

      override fun onCreateView(
      inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?
      ): View? {
      val view = inflater.inflate(R.layout.fragment_receiver, container, false)

      val model = ViewModelProviders.of(activity!!).get(
      tv_msg = view.findViewById( as TextView

      model.message.observe(this, object : Observer {
      override fun onChanged(o: Any?) {
      tv_msg.text = o!!.toString()
      return view

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.