appeldaniel
appeldaniel

Reputation: 123

How to run a function when navController.popBackStack() is called in Jetpack Compose?

I have a list of "clients" which are presented using a lazy column on a screen. I am able to enter each client (navigate to another screen) and edit the information of this client, but when I go back to the list of clients, the client information is not updated. The server and DB are working as expected, but I cant seem to figure out how to trigger my "loadClients" method when navigating back.. Can anyone suggest a way?

I have tried this (as per chat gpt), but this doesnt work either (the loadNextClients() is the function I would like to run (it sends an http request for the updated clients)):

val navBackStackEntry by navController.currentBackStackEntryFlow.collectAsState(initial = navController.currentBackStackEntry) // Observe back stack entry changes
    LaunchedEffect(navBackStackEntry) {
        // Check if this is the current destination
        if (navBackStackEntry?.destination?.route == Screens.ClientsMainScreen.route || navBackStackEntry?.destination?.route == Screens.ClientsNavGraph.route) {
            viewModel.loadNextClients()
        }
    }

I have scoured the internet and cant seem to find an answer any suggestions or help whatsoever would be greatly appreciated

Upvotes: 2

Views: 90

Answers (0)

Related Questions