PhilippeBoisney / ArchApp

Simple Android app to show how to design a multi-modules MVVM Android app (fully tested)
623 stars 98 forks source link

Get result or callback from destination #5

Open nickstamp93 opened 4 years ago

nickstamp93 commented 4 years ago

Let's say i have a destination (bottom sheet, fragment) that adds a new item in the repository. How should i get a callback or a results from it?

hamedsilver commented 4 years ago

@nickstamp93 in new version of navigation you can pass data back to your previous destination this feature added in 2.3.0-alpha03 you can send data from destination B like this: navController.previousBackStackEntry?.savedStateHandle?.set("key", result) and get data in destination A like this:

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