previousBackStackEntry does not function consistently

Viewed 28

I've been stuck with the following problem for 2 days now:

I have Composable Main, from where I navigate to a dialog:

@Composable
fun Main(navController: NavHostController) {

val mainViewModel: MainViewModel = viewModel()
val mainViewState by mainViewModel.state.collectAsState()

val placeResult = navController.currentBackStackEntry
        ?.savedStateHandle
        ?.getLiveData<String>("suggestion")?.observeAsState()

placeResult?.value?.let {

        if (it.isNotEmpty()) {

            setplace()

        }

        }

if (mainViewState.showSearchPlace) {

        mainViewModel.showSearchPlace(false)
        navController.navigate(Destinations.SearchPlace)

    }
}

The SearchPlace dialog is defined in NavHost like this:

dialog(Destinations.SearchPlace, dialogProperties = DialogProperties(usePlatformDefaultWidth = false,
dismissOnBackPress = true,
                dismissOnClickOutside = true)) {
                SearchPlaceDialog(
                navController)
            }

And this is how I close SearchPlaceDialog to return to Main:

navController.previousBackStackEntry?.savedStateHandle?
.set("suggestion", viewState.suggestions.suggestions[index].toNavArg())
navController.popBackStack()

Everything works fine: when I first go to SearchPlaceDialog, it correctly passes the value back to Main, which calls 'setplace()'. If I do this a second, third, fourth time, 'setplace()' is not called immediately. 'setplace()' is then only called when I navigate back to SearchPlaceDialog. I just can't find what the problem is here.

0 Answers
Related