DEV Community

Discussion on: Sharing data between ViewModels in Compose

Collapse
 
rafa_b8907973ce09a8a0e663 profile image
Rafa

Hi Aleksei! thanks for the post, I have a doubt though: when we want to pass data to the previous screen, isn't working to do in composable2: navController.previousBackStackEntry?.savedStateHandle?.set("key", it) and then in the viewmodel1 getting the value by: savedStateHandle.get("key").
Any way to get it back there in viewmodel1? thanks!

Collapse
 
mardsoul profile image
Aleksei Laptev

Hello
Didn't understand. More details, pls.

Collapse
 
rafa_b8907973ce09a8a0e663 profile image
Rafa

If you know how to navigate back from Screen2 to Screen1 and send data back. I.e.: in screen two the user types a text and once it presses system's back button, it navigates back to screen one and that text he typed earlier has to appear now in a textfield in screen1.

Thread Thread
 
mardsoul profile image
Aleksei Laptev • Edited

You can use BackHandler, it overrides system back button. Here you are an example with navigation with arguments.
As I mentioned in the article it's not the best practice, but like a variant... Better try another approach.