Android – Passing Data Between Fragments

In this article, we’ll learn how to pass data between fragments in Android. I will also explain, what is possible ways of communicating between two fragments in Android. There’s always need for communication between fragment.

android-logo

How to communicate between Fragments

We know Fragment is a reuse UI component, It should be built self-contained, modular that defines its own layout and behavior. Once you have written these reusable Fragments, you can associate them with an Activity.

Normally, two Fragments should never communicate directly. There’s always need for communication between Fragments, and this is a very common requirement in the real project, for example, to change the content based on a user event. etc.

Four ways to communication between Fragments

Android has several ways to do that. In this article, We will explain all the possible ways as below:

  • Interface
  • ViewModel
  • RxJava
  • Event Bus
1. Interface

The interface is the simplest way to communicating between two fragments in android. In this approach, we can define an interface in the Fragment class and implement it in Activity. whoever conforms to that interface, can be informed by the Fragment of events. Basically, Fragment capture the interface implementation onAttach() lifecycle method. For Example :
/**
   * This interface must be implemented by activities that contain this
   * fragment to allow an interaction in this fragment to be communicated
   * to the activity
   */
  public interface OnFragmentCommunicationListener {
    void onNameChange(String name);
    void onEmailChange(String email);
  }


Attached with Activity

@Override
  public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentCommunicationListener) {
      mListener = (OnFragmentCommunicationListener) context;
    } else {
      throw new RuntimeException(context.toString()
          + " must implement OnFragmentCommunicationListener");
    }
  }
  @Override
  public void onDetach() {
    super.onDetach();
    mListener = null;
  }

2. ViewModel

ViewModel is a recommended way by Google to communicate between fragments. In this approach, we create a shared ViewModel object. Both fragments can access the ViewModel instance and this instance(ViewModel) is associate fragments containing Activity.

3. RxJava

This way is pretty good and easy to implement. Personally, This is the best way to communicating two fragments in Android. In this technique, We use PublishSubject to create our own RxBus. This PublishSubject emits all the subsequent items of the source Observable.

4. Event Bus

EventBus is a publish or subscribe event bus for Android and Java. It simplifies communication between components. Personally, I don’t like this technique because It very loosely coupled and every broadcast can listen to events from a singleton instance. When the project very scalable then makes it very hard to maintain.
Conclusion
 
In this article, We understood the different ways to passing the data between fragments in android.

Thanks for reading ! I hope you enjoyed and learned about different fragment communication ways in Android. Reading is one thing, but the only way to master it is to do it yourself.
 
If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING!!😊😊😊

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s