---
title: "Starting a Fragment for results in Android"
description: "Google has just added a new ability to FragmentManager which made the FragmentManager be able to act as a central store for fragment results."
canonical: https://oozou.com/blog/starting-a-fragment-for-results-in-android-46
locale: en
date: 2020-06-21
author: "OOZOU Team"
---

# Starting a Fragment for results in Android

> Google has just added a new ability to FragmentManager which made the FragmentManager be able to act as a central store for fragment results.

Published: 2020-06-21 · Author: OOZOU Team (Engineering) · 4 min read

As an Android developer, we all should be familiar with the **startActivityForResult** function. We use this function for starting a new Activity and getting results back to the current Fragment or Activity, thereby facilitating communication between a fragment and an activity. However, when the screens in my application are represented by Fragments, and I can’t use **startActivityForResult** for passing results between them.

Until recently, when it comes to the communication between Fragments there is nothing similar to **startActivityForResult**. Therefore, I had to create shared **ViewModel** between Fragments so that I can pass data back and forth between them. This new ability allows us to handle results between fragments in a manner similar to how we handle results between activities using startActivityForResult, but specifically designed for a result within the fragment context.

Fortunately, Google has just added a new ability to the Fragment family. There are two approaches to pass data back and forth between Fragments; first is by using **FragmentManager** and second is by using **the Navigation component library**.

Then let’s see how we can use this new ability in action. I will build a simple note list application with two Fragments with the Navigation component library. One fragment for displaying the list of notes and another one for taking note.

Let's start by adding important dependencies. By the time this article is written the latest Fragment version is **1.3.0-alpha05**.

```groovy
def nav_version = "2.3.0-beta01"
def fragment_version = "1.3.0-alpha05"

implementation "androidx.fragment:fragment:$fragment_version"
implementation "androidx.fragment:fragment-ktx:$fragment_version"

implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
```

Here is the navigation graph for this project.

All of the source code is available at [https://github.com/BenBoonya/fragment-result](https://github.com/BenBoonya/fragment-result)

# Using FragmentManager

Source code: [https://github.com/BenBoonya/fragment-result/tree/using-fragment-manager](https://github.com/BenBoonya/fragment-result/tree/using-fragment-manager)

The new ability added to **FragmentManager** made the **FragmentManager** be able to act as a central store for Fragment results. Although the function is not named **startFragmentForResult**, it acts similarly to **startActivityForResult**. With the snippet below, we can pass the data back and forth between Fragments easily. This ability has been added since [Fragment 1.3.0-alpha04](https://developer.android.com/jetpack/androidx/releases/fragment#1.3.0-alpha04).

For **NoteListFragment** to get the result back from **AddNoteFragment**. We have to set a result listener. Let's take a look at the **NoteListFragment**.

```kotlin
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    <em>setFragmentResultListener</em>(
        ADD_NOTE
    ) <strong>{ </strong>_, result <strong>->
        </strong>result.getString(NOTE)?.<em>let </em><strong>{ </strong>note <strong>->
            </strong>adapter.addItem(note)
        <strong>}
    }
</strong>}
```

As you can see, there is an extension function called **setFragmentResultListener** provided. We get the result back as a bundle.
Moving on to the **AddNoteFragment**. We need to set result, and it can be done easily as follow.

```kotlin
private fun navigateBack() {
    <em>setFragmentResult</em>(
        NoteListFragment.ADD_NOTE, <em>bundleOf</em>(
            NoteListFragment.NOTE <em>to </em>binding.editText.<em>text</em>.toString()
        )
    )
    <em>findNavController</em>().popBackStack()
}
```

The only thing to keep in mind is that you need to set the result on the same **FragmentManager** using the same requestKey. If we take a closer look into the Fragment extension functions, we can see that both **setFragmentResult**, **setFragmentResultListener** are calling on **parentFragmentManager** in the following snippets.

```kotlin
inline fun Fragment.setFragmentResultListener(
    requestKey: String,
    crossinline listener: ((resultKey: String, bundle: Bundle) -> Unit)
) {
    <em>parentFragmentManager</em>.<em>setFragmentResultListener</em>(requestKey, this, listener)
}
```
```kotlin
fun Fragment.setFragmentResult(
    requestKey: String,
    result: Bundle
) = <em>parentFragmentManager</em>.setFragmentResult(requestKey, result)
```

Another thing that worth mentioning is there can only be a single listener and result for a given key. In our example, if we call **setFragmentResult** in **AddNoteFragment** more than once the system will send the most recent result to **NoteListFragment** before the **AddNoteFragment** is popped off the back stack.
If the **AddNoteFragment** happens to be a child Fragment of the **NoteListFragment**. We just need to set the listener and result on the **childFragmentManager** instead of **parentFragmentManager**.

# Using the Navigation component library

Source code: [https://github.com/BenBoonya/fragment-result/tree/using-navigation-component](https://github.com/BenBoonya/fragment-result/tree/using-navigation-component)

To return results to previous destinations, the Navigation component library uses the **SavedStateHandle** which is a key-value map that can be used to store and retrieve data persistently through configuration changes
On **NoteListFragment**:

```kotlin
override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    <em>findNavController</em>().<em>currentBackStackEntry</em>?.<em>savedStateHandle</em>?.getLiveData<String>(
        ADD_NOTE
    )?.observe(<em>viewLifecycleOwner</em>, <em>Observer </em><strong>{ </strong>note <strong>->
        </strong>adapter.addItem(note)
    <strong>}</strong>)
}
```

The **_savedStateHandle_** provides a LiveData and we can put the observer on it to get the value.
On **AddNoteFragment**

```kotlin
<em>findNavController</em>().<em>previousBackStackEntry</em>?.<em>savedStateHandle</em>?.set(
    NoteListFragment.ADD_NOTE,
    binding.editText.<em>text</em>.toString()
)
```

Here we set the value to the **_savedStateHandle_** of the previous back stack entry which belongs to the **NoteListFragment**

# Conclusion

I'm excited to see the stable release of these features. In my opinion, the added ability of the Fragment family made working with the Single activity architecture which has all of the destinations represented by Fragment become much easier and more desirable.

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
