Coder Social home page Coder Social logo

mohamadkotb / navigationresult Goto Github PK

View Code? Open in Web Editor NEW

This project forked from phelat/navigationresult

0.0 0.0 0.0 182 KB

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

License: Apache License 2.0

Kotlin 100.00%

navigationresult's Introduction

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

navigationresult's People

Contributors

karreprakash avatar phelat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.