Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 679
CodingBruceLee Avatar asked Dec 16 '25 14:12

CodingBruceLee


1 Answers

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.

like image 114
ianhanniballake Avatar answered Dec 19 '25 07:12

ianhanniballake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!