Hello, Does Anyone have idea on how to clear the ViewModel if any composable screen associated with ...
s
Hello, Does Anyone have idea on how to clear the ViewModel if any composable screen associated with that VM is not visible when other screen is Opened ? The issue is When the ABC screen is open I am doing the API call inside Init block of ViewModel. After switching the screen again when the screen is opened the init block is not called and No updated Api call happens
a
You need to put your composable in different navgraph, no new viewmodel instance will be create d in same graph.
s
Right new I have this Screen as a part of BottomBar. Is this possible in this case ?
a
Perhaps one of the side effect APIs can help?
s
Are you talking of DisposableEffect ?
a
Something like that. DisposableEffect is for disposing or destructing, afaik. I believe a similar API would exist for construction as well. You could use that on your screen-level composable. Please bear in mind I'm very new to compose and especially these side effect APIs.
s
No worries.
k
LaunchEffect
is what you are probably looking for. Just call a method on your
ViewModel
inside of it that refreshes your data.
K 2
💯 2
s
Thanks @kotlinforandroid. Working as expected. Thanks for suggestions others as well
👍 2