OOZOU
Get in Touch
Back to Blog

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.

June 21, 2020
4 min read
Starting a Fragment for results in Android

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.

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

Using FragmentManager

Source code: 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.

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

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.

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.

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

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:

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

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

Related Articles

How Can I Design a Software: A Beginners Guide for Business Owners

How Can I Design a Software: A Beginners Guide for Business Owners

Designing software is an exciting yet complex process that involves creativity, logical thinking, and problem-solving skills.

Read More →
The Future of Edge Computing: What Businesses Need to Know

The Future of Edge Computing: What Businesses Need to Know

The digital landscape is evolving rapidly, and businesses must adapt to keep up with rising demands for speed, efficiency, and real-time processing.

Read More →
The Future of Retail: AI, AR, and Digital Transformation Trends

The Future of Retail: AI, AR, and Digital Transformation Trends

Retail is evolving at an unprecedented pace, driven by AI (Artificial Intelligence), AR (Augmented Reality), and digital transformation technologies, revolutionizing the retail industry. Traditional brick-and-mortar stores are no longer just about selling products—they are becoming smart, interactiv

Read More →

Have a Project in Mind?

Let's discuss how we can help bring your ideas to life with our expertise in web and mobile development.

Start a Conversationhello@oozou.com
OOZOU Logo

Bangkok · Singapore · Hong Kong

X
Facebook
Instagram
LinkedIn

Services

  • Web Development
  • AI Agents & Generative AI
  • Mobile App Development
  • Data Analytics & Engineering
  • UI/UX & Product Design
  • Digital Transformation

Company

  • About Us
  • Careers
  • Blog
  • Whitepapers
  • Case Studies
  • Today I Learned
  • Contact

More

  • Industries
  • Get an Estimate
  • Partner Network
© 2026 OOZOU. All rights reserved.
Privacy PolicyCode of ConductABAC Policy