How to get a result from fragment using Navigation Architecture Component? How to get a result from fragment using Navigation Architecture Component? android android

How to get a result from fragment using Navigation Architecture Component?


They have added a fix for this in the 2.3.0-alpha02 release.

If navigating from Fragment A to Fragment B and A needs a result from B:

findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<Type>("key")?.observe(viewLifecycleOwner) {result ->    // Do something with the result.}

If on Fragment B and need to set the result:

findNavController().previousBackStackEntry?.savedStateHandle?.set("key", result)

I ended up creating two extensions for this:

fun Fragment.getNavigationResult(key: String = "result") =    findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<String>(key)fun Fragment.setNavigationResult(result: String, key: String = "result") {    findNavController().previousBackStackEntry?.savedStateHandle?.set(key, result)}


Since Fragment KTX 1.3.0-alpha04 Android supports passing data between fragments or between fragments and activities. It's similar to startActivityForResult logic.

Here is an example with Navigation Component. You can read more about it here

build.gradle

implementation "androidx.fragment:fragment-ktx:1.3.0-alpha04"

FragmentA.kt

class FragmentA : Fragment() {    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {        // Step 1. Listen for fragment results        setFragmentResultListener(FragmentB.REQUEST_KEY) { key, bundle ->             // read from the bundle        }        // Step 2. Navigate to Fragment B        findNavController().navigate(R.id.fragmentB)    }}

FragmentB.kt

class FragmentB : Fragment() {    companion object {        val REQUEST_KEY = "FragmentB_REQUEST_KEY"    }    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {        buttonA.setOnClickListener { view ->             // Step 3. Set a result            setFragmentResult(REQUEST_KEY, bundleOf("data" to "button clicked"))            // Step 4. Go back to Fragment A            findNavController().navigateUp()        }    }}    


According to Google: you should try to use shared ViewModel. Check below example from Google:

Shared ViewModel that will contain shared data and can be accessed from different fragments.

public class SharedViewModel extends ViewModel {    private final MutableLiveData<Item> selected = new MutableLiveData<Item>();    public void select(Item item) {        selected.setValue(item);    }    public LiveData<Item> getSelected() {        return selected;    }}

MasterFragment that updates ViewModel:

public class MasterFragment extends Fragment {    private SharedViewModel model;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);        itemSelector.setOnClickListener(item -> {            model.select(item);        });    }}

DetailsFragment that uses shared ViewModel:

public class DetailFragment extends Fragment {    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);        model.getSelected().observe(this, item -> {           // Update the UI.        });    }}