koin
koin copied to clipboard
Compose create new instance for the same composable func
Describe the bug
Let's assume, I have a composable func name ScreenFlowContent
which renders some ui AND allows to open another instance of ScreenFlowContent
. If I use code like this:
val screenModule = module {
viewModel { ScreenVm() }
}
class ScreenVm : ViewModel()
@Composable
fun ScreenFlowContent(id: String, args: Bundle?) {
val vm = getViewModel<ScreenVm>()
}
Im getting the same instance of vm on each ScreenFlowContent
func invocation. I expect it to be different.
Another case. Lest say, we have navigation like this: ScreenA -> ScreenB -> ScreenFlowContent
, then go back to ScreenA
AND navigate forward to ScreenFlowContent
. I again see the same instance on ScreenVm
that was before.
I expect vm to be cleaned up after I leave my screen completely. Not sure if it's possible with koin now. I have an unique key for each screen composable and tried to find a way to remember vm using this key. but no luck. plus I didnt find how to force vm to be destroyed once I leave my screen composable.
Koin version being used - 3.1.4
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Same problem with 3.2.0-beta-1
Here the problem is your ScreenVm
is tied to the underlying Activity. A viewModel is tied to the associated ViewModelStoreOwner, here your activity (by default).
New ViewModel fixes allow to better use qualifiers edaa00270ad58b1934f276062d852930dddb4e70
. It would solve your issue here.