Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.
/ NavigationResult Public archive

startActivityForResult but for fragments! (Addon for Jetpack's Navigation component)

License

Notifications You must be signed in to change notification settings

PHELAT/NavigationResult

Repository files navigation

NavigationResult

startActivityForResult but for fragments! (Addon for Jetpack's Navigation component).
🗒 You can read a blog post about this library from here.

How to use

  1. Add NavigationResult's dependency into your build.gradle file:
dependencies {
    implementation "com.phelat:navigationresult:[latest_version]"
}
  1. Extend your starting point fragment from BundleFragment
class FragmentA : BundleFragment() {
    ...
}
  1. Navigate to destination fragment using navigate function:
navigate(FragmentADirections.fragmentAToFragmentB(), REQUEST_CODE)
// If you aren't using SafeArgs plugin, you can navigate with direction id
navigate(R.id.fragmentAToFragmentB, REQUEST_CODE)
  1. Override onFragmentResult in your starting point fragment:
class FragmentA : BundleFragment() {

    override fun onFragmentResult(requestCode: Int, bundle: Bundle) {
        // Do whatever you want with result bundle from the destination fragment
    }
    
}
  1. In your destination fragment use navigateUp extension function:
navigateUp(REQUEST_CODE, Bundle().apply {
    putBoolean("isLoginSuccessful", true)
})
  1. Extend your activity from FragmentResultActivity:
class MainActivity : FragmentResultActivity() {
    // Return the id of your NavHostFragment
    override fun getNavHostFragmentId(): Int = R.id.nav_host_fragment
}

Two ways for defining request code

  1. You can pass request code through navigate function:
navigate(FragmentADirections.fragmentAToFragmentB(), REQUEST_CODE)
// OR
navigate(R.id.fragmentAToFragmentB, REQUEST_CODE)
  1. You can also set request code in your navigation graph xml file:
<fragment
    android:id="@+id/fragment_a"
    android:name="com.sample.FragmentA">
    <action
        android:id="@+id/a_to_b"
        app:destination="@id/fragment_b">
        <argument
            android:name="fragment:requestCode"
            android:defaultValue="1000"
            app:argType="integer" />
    </action>
</fragment>

If you don't want to set default value for fragment:requestCode:

navigate(FragmentADirections.fragmentAToFragmentB(10000))
// OR
navigate(R.id.fragmentAToFragmentB, Bundle().apply {
    putInt("fragment:requestCode", 1000)
})

Update NavHostFragment's id

You can update NavHostFragment's id at runtime using updateNavHostFragmentId function:

// This function is only available for FragmentResultActivity
updateNavHostFragmentId(R.id.anotherNavHostFragment)

Navigate with request code from FragmentResultActivity

You can navigate with request code from FragmentResultActivity as you would do using BundleFragment.

Navigate up from <dialog/> destinations

This library does support <dialog/> destinations and you can use them normally without doing anything extra.

Sample

You can checkout the sample project for NavigationResult from here

About

startActivityForResult but for fragments! (Addon for Jetpack's Navigation component)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages