Communicate between fragments and activity using LiveData

Yashish Dua
AndroidPub
Published in
3 min readNov 17, 2018

--

This article is split into two parts —
I. Communication between fragments
II. Communication between an activity and fragment

We will first look at the primitive ways of doing these and what certainly is not good in these ways. Moving forward we will see the new and optimized way to handle these communications using ViewModel and LiveData.

I. Communication between fragments

Earlier to communicate between fragments, we had to pass data from one fragment to parent activity and then from this parent activity to another fragment using interface callbacks. This was the only effective way to handle communication since activity has the context and child fragments uses that context to sync along the life cycle.

Let’s consider an example where we want to pass data from WallFragment to WallDetailFragment using interface callbacks. The first step is to create an interface(some call it a contract) with an abstract function handleOnClick.

Next step is to attach the parent activity to this listener and then we can call handleOnClick on this fragmentListener to pass flow to the activity which has implemented that interface/contract.

From MainActivity we then call the function of another Fragment to pass data to it.

Pain Points in this primitive way:

  1. Using interface callbacks ends up being more work to implement and it is not easily reusable in other Fragments.
  2. Both fragments need to create the interface if communication is in both ways(I think this is totally redundant!)
  3. Explicitly we need to take care of both fragments’ life cycle synchronization with parent activity since the parent activity is binding the two fragments.

Here comes the LiveData to rescue!

With the release of architecture components, we can handle all these pain points by creating a Shared ViewModel and expose the data stream using LiveData. (Read more about ViewModel and LiveData)

This is our extended ViewModel, SharedViewModel class.

Now, in our fragments, we will start observing for the changes in the data by calling the ViewModel with the same activity scope.

That’s it! We just got rid of too much of boilerplate code, issue of coupling, and yes ViewModel takes care of the lifecycle and observes data when needed.

II. Communication between activity and fragment

The same primitive way of interface callbacks also applies here. However, the pain points can be avoided here if you keep the advantages of ViewModel aside. You can pretty fairly achieve a good efficient structure with interface callbacks since here you just need to communicate between an activity and its child fragment.
But the recommended way to achieve this type of communication is still using LiveData and ViewModel due to their advantages including lifecycle awareness and reduced boilerplate code.

You just need to observe the Live Data on the View Model and you are good to go!

Major Advantage

We can use the same ViewModel at multiple places to observe the changes made anywhere inside the application. Also, we can observe the emitted data via LiveData at all places which are observing for the data update. This is a n is to m relationship.

--

--