0

My app starts with NewAdFragment and inside this fragment, There is a button that allows user to choose the category, When the user clicks on the button I'll navigate him to ChooserFragment with passing categories list as an argument, There is RecyclerView inside this fragment and it shows categories.

What do I want? I want when the user chooses an item from RecyclerView, Then it must back to NewAdFragment with specific data.

I can solve this problem in a lot of ways but maybe there is a method that exists in the navigation component library already.

What is the best way to do that?

Taha Sami
  • 1,565
  • 1
  • 16
  • 43

1 Answers1

6

try to implement below code blocks as mentioned here:

FRAGMENT A

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val navController = findNavController();
// We use a String here, but any type that can be put in a Bundle is supported
navController.currentBackStackEntry?.savedStateHandle?.getLiveData<String>("key")?.observe(
    viewLifecycleOwner) { result ->
    // Do something with the result.
  }
}

FRAGMENT B

navController.previousBackStackEntry?.savedStateHandle?.set("key", result)
navController.popBackStack()

you can also check this

aligur
  • 3,387
  • 3
  • 34
  • 51