Proper Fragment Communication in Android

Don’t let the system hang up on your fragment’s listener

Proper Fragment Communication in Android

You may have already read tons of articles covering this topic. It’s legitimate to wonder why I’d write an additional one.

I’m mentioning the proper way when, in fact, several solutions exist. The Android ecosystem has evolved since its beginnings, and you may wish to leverage ViewModels or even third-party libraries such as RxJava or EventBus.

If you are interested in knowing more about ViewModels, I’d recommend checking out this excellent article:

Communicate between fragments and activity using LiveData

Although all of the above present advantages, you may be in a situation where you are unable to use them. You’re then back to square one with the good old-fashioned interface solution.

With that said, working with interfaces works perfectly and remains the lightest approach possible, as it doesn’t require any third-party library.

However, developers often fail to configure them properly.

Fragment-to-Activity Communication

Let’s assume you have an activity embedding a fragment. Upon a specific event happening on your fragment, you want to notify your activity so that it reacts to this event.

Here is a basic example with a fragment declaring an interface to propagate events to anyone implementing it:

And the activity implementing the interface:

To settle the contract, the activity must declare itself to the fragment as a listener.

The question is: where do we call this method? You may, for instance, choose to call it inside the onCreate method.

What about if the fragment gets destroyed? Is the activity still listening to the fragment’s events?

The answer is no if the system, for any reason, destroyed the fragment and not the activity. Your activity will create another fragment instance but won’t re-attach the listener to it. No events from the fragment will be propagated to your activity, leaving it in a corrupted state.

To properly handle all cases, you must bind your listener when your activity is notified your fragment has been attached:

With this configuration, you tightly couple your activity with your fragment.

Nested Fragment Communication

If you happen to have a fragment hosting another fragment, you can use the same mechanism to communicate between those two fragments. Fragments also offer an onAttachFragment callback to notify you when your child fragment is attached.

Please Use the Proper Attach Callback

The onAttachFragment callback was recently added in API 24, then backported with the newest androidX support package. I still come across the usage of the former onAttach method inside the fragment that defines the interface.

Although both solutions are valid, I’d recommend using the newest, as it prevents the fragment from knowing which components implement its interface. Fragments should not be responsible for attaching their own listener.

Moreover, by doing this, you force the fragment’s caller to implement its interface — even though the caller doesn’t need to react to the fragment events. It’s simply bad architecture.

Anyway, the Android team updated the official documentation in that favor.

Thoughts About Fragment Communication

I’ve had the chance to experiment with all of the solutions above. So far, I’ve been loyal to interfaces, as they provide a consistent mechanism — whether I need to communicate between fragments or between a fragment and an activity.

No matter how you proceed, you’ll be fine as long as you understand how a fragment’s lifecycle works.

comments powered by Disqus