To reuse the Fragment UI components, you should build each as a completely self-contained, modular component that defines its own layout and behavior. Once you've defined these reusable Fragments, you can associate them with an Activity and connect them with the application logic to realize the overall composite UI.
Often you will want one Fragment to communicate with another, for example to
change the content based on a user event. All Fragment-to-Fragment
communication is done either through a shared
ViewModel
or through the associated Activity. Two Fragments should never communicate
directly.
The recommended way to communicate between fragments is to create a shared
ViewModel. Both fragments can
access the ViewModel through their containing Activity. The Fragments can
update data within the ViewModel and if the data is exposed using
LiveData, the new state will be
pushed to the other fragment as long as it is observing the LiveData
from the ViewModel. To see how to implement this kind of communication,
read the 'Share data between Fragments' section in the
ViewModel guide.
If you are unable to use a shared ViewModel to communicate between your
Fragments, you can manually implement a communication flow using interfaces.
However this ends up being more work to implement and it is not easily
reusable in other Fragments.
Define an interface
To allow a Fragment to communicate up to its Activity, you can define an
interface in the Fragment class and implement it within the Activity. The
Fragment captures the interface implementation during its onAttach()
lifecycle method and can then call the Interface methods to communicate
with the Activity.
Here's an example of Fragment to Activity communication:
HeadlinesFragment
Kotlin
class HeadlinesFragment : ListFragment() {
internal var callback: OnHeadlineSelectedListener
fun setOnHeadlineSelectedListener(callback: OnHeadlineSelectedListener) {
this.callback = callback
}
// This interface can be implemented by the Activity, parent Fragment,
// or a separate test implementation.
interface OnHeadlineSelectedListener {
fun onArticleSelected(position: Int)
}
// ...
}
Java
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener callback;
public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener callback) {
this.callback = callback;
}
// This interface can be implemented by the Activity, parent Fragment,
// or a separate test implementation.
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
// ...
}
MainActivity
Kotlin
class MainActivity : Activity(), HeadlinesFragment.OnHeadlineSelectedListener {
// ...
fun onAttachFragment(fragment: Fragment) {
if (fragment is HeadlinesFragment) {
fragment.setOnHeadlineSelectedListener(this)
}
}
}
Java
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
// ...
@Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof HeadlinesFragment) {
HeadlinesFragment headlinesFragment = (HeadlinesFragment) fragment;
headlinesFragment.setOnHeadlineSelectedListener(this);
}
}
}
Now the fragment can deliver messages to the activity by calling the
onArticleSelected() method (or other methods in the interface) using the
mCallback instance of the OnHeadlineSelectedListener interface.
For example, the following method in the fragment is called when the user clicks on a list item. The fragment uses the callback interface to deliver the event to the parent activity.
Kotlin
override fun onListItemClick(l: ListView, v: View, position: Int, id: Long) {
// Send the event to the host activity
callback.onArticleSelected(position)
}
Java
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
callback.onArticleSelected(position);
}
Implement the interface
To receive event callbacks from the fragment, the activity that hosts it must implement the interface defined in the fragment class.
For example, the following activity implements the interface from the above example.
Kotlin
class MainActivity : Activity(), HeadlinesFragment.OnHeadlineSelectedListener {
...
fun onArticleSelected(position: Int) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
Java
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
Deliver a Message to a Fragment
The host activity can deliver messages to a fragment by capturing the
Fragment instance with
findFragmentById(),
then directly call the fragment's public methods.
For instance, imagine that the activity shown above may contain another fragment that's used to display the item specified by the data returned in the above callback method. In this case, the activity can pass the information received in the callback method to the other fragment that will display the item:
Kotlin
class MainActivity : Activity(), HeadlinesFragment.OnHeadlineSelectedListener {
...
fun onArticleSelected(position: Int) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
val articleFrag = supportFragmentManager.findFragmentById(R.id.article_fragment) as ArticleFragment?
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position)
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
supportFragmentManager.commit {
// Replace whatever is in the fragment_container view with a new Fragment, generated
// from the FragmentFactory, and give it an argument for the selected article
replace<ArticleFragment>(R.id.fragment_container, null, bundleOf(ArticleFragment.ARG_POSITION to position))
// add the transaction to the back stack so the user can navigate back
addToBackStack(null)
}
}
}
}
Java
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
}
To learn more about implementing Fragments, see Fragments. You can also learn more by exploring the FragmentBasics sample app.

