Search code examples
androidandroid-jetpackandroid-architecture-navigationandroid-navigation-graph

How to get a result from Activity to Fragment With Navigation Component?


I have MainActivity, ProductListFragment and ProductDetailActivity.
ProductListFragment is a start fragment.
when I click a item of list in ProductListFragment, it goes to DetailActivity with action that i set in nav_graph.

  val action = ProductListFragmentDirections.actionProductListFragmentToProductDetailActivity(productId)
  findNavController().navigate(action)

and when I click up-button or press back-button, DetailActivity is finished() and it goes back to ProductListFragment.
in this case, i want to get a result when the DetailActivity is finished() like onActivityResult() callback in ProductListFragment.
so what kind of method should i use to get a result???
is there any way to get a result by using Navigation component or findNavcontroller?


Solution

  • If you want to start an Activity and receive a result back from that Activity, you should use the Activity Result APIs.

    This is the one and only correct API to use for this particular case - there is no Navigation specific wrapper around the Activity Result APIs.