Core Android

Passing Data Between Fragments

In this post, we’ll learn how to pass data between fragment 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.

How to communicate between Fragments

You guys well 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 tutorial, I will tell all the possible ways. If you have any feedback or improvement suggestions are more than welcome.

  • Interface
  • ViewModel
  • RxJava
  • Event Bus

You can add here If I have missed. These are the best ways for communicating between one fragment to another fragment. In this article, I will explain only using Interface. For other ways, I will write a separate article.

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. Somethings like that

  /**
   * 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;
  }

I have written a separate article on Fragment communication using Interface that gives more insight into this.

2. ViewModel

ViewModel is 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.

I have written a separate article on this approach the below post gives more insight into this.

Read this approach Fragment communication using ViewModel in Android

3. RxJava

This way is pretty good and easy to implement. Personally, I feel 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.

Read more about Fragment communication using RxJava in Android

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 event from a singleton instance. When the project very scalable then makes it very hard to maintain.

Deep drive on Fragment communication using EventBus in Android

https://paypal.me/
droidwave?locale.x=en_GB

Leave a Reply

2000