如何使用导航体系结构组件从片段获得结果?

假设我们有两个片段: MainFragmentSelectionFragment。第二个是为选择某个对象而构建的,例如一个整数。接收第二个片段的结果有不同的方法,如回调、总线等。

现在,如果我们决定使用导航架构组件来导航到第二个片段,我们可以使用以下代码:

NavHostFragment.findNavController(this).navigate(R.id.action_selection, bundle)

其中 bundleBundle的一个实例(当然)。正如您所看到的,没有访问 SelectionFragment的途径,我们可以在其中放置一个回调。问题是,如何使用导航体系结构组件接收结果?

48713 次浏览

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.
});
}
}

I would suggest you to use NavigationResult library which is an add-on for JetPack's Navigation Component and lets you to navigateUp with Bundle. I've also wrote a blog post about this on Medium.

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.6 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.6"

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()
}
}
}

with a little improvement of @LeHaine 's answer, you can use these methods for navigation 2.3.0-alpha02 and above

fun <T> Fragment.getNavigationResult(key: String) =
findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<T>(key)


fun <T> Fragment.setNavigationResult(result: T, key: String) {
findNavController().previousBackStackEntry?.savedStateHandle?.set(key, result)
}

Use these extension functions

fun <T> Fragment.getNavigationResult(key: String = "result") =
findNavController().currentBackStackEntry?.savedStateHandle?.get<T>(key)


fun <T> Fragment.getNavigationResultLiveData(key: String = "result") =
findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<T>(key)


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

So if you want to send result from Fragment B to fragment A

Inside Fragment B

setNavigationResult(false, "someKey")

Inside Fragment A

val result = fragment.getNavigationResultLiveData<Boolean>("someKey")
result.observe(viewLifecycleOwner){ booleanValue-> doSomething(booleanValue)

Important note

In the Fragment B you need to set the result (setNavigationResult()) in the started or resumed state (before onStop() or onDestroy()), otherwise previousBackStackEntry will be already null.

Important note #2

If you’d only like to handle a result only once, you must call remove() on the SavedStateHandle to clear the result. If you do not remove the result, the LiveData will continue to return the last result to any new Observer instances.

More information in the official guide.

Just an alternative to the other answers...

EventBus with MutableShareFlow as it core in a shared object (ex: repo) and an observer describe in here

Looks like things are moving away from LiveData and going in Flow direction.

Worth to have a look.

I created a wrapper function that's similar to LeHaine's answer, but it handles more cases.

To pass a result to a parent from a child:

findNavController().finishWithResult(PickIntervalResult.WEEKLY)

To get result from a child in a parent:

findNavController().handleResult<PickIntervalResult>(
viewLifecycleOwner,
R.id.navigation_notifications, // current destination
R.id.pickNotificationIntervalFragment // child destination
) { result ->
binding.textNotifications.text = result.toString()
}

My wrapper isn't so simple as LeHaine's one, but it is generic and handles cases as:

  1. A few children for one parent
  2. Result is any class that implements Parcelable
  3. Dialog destination

See the implementation on the github or check out an article that explains how it works.

fun <Type> BaseNavigationActivity<*,*,*>.getNavigationResult(key : String, @IdRes viewId: Int)  =
this.findNavController(viewId).currentBackStackEntry?.savedStateHandle?.getLiveData<Type>(key)




fun <Type> BaseNavigationActivity<*,*,*>.setNavigationResult(result: Type, key: String, @IdRes viewId: Int){
this.findNavController(viewId).previousBackStackEntry?.savedStateHandle?.set<Type>(key, result)
}