I have a compose app and am struggling with best p...
# koin
m
I have a compose app and am struggling with best practices for using koin’s
getViewModel()
from within a compose
Dialog
. Since the viewmodel is scoped to the fragment, the same viewmodel instance is returned each time (furthermore, this is regardless of any
parametersOf
args). So, how are we supposed to deal with this? I want to get a new instance every time the user taps on whatever to bring up the
Dialog
. Only the same instance should be returned with there is a config change etc. This is particularly an issue when the dialog contains a text field, because consider when the user makes some edit to the text and then hits cancel. By default, the dialog will show the old cancelled text when next invoked, unless we take care to reset all relevant
MutableState
which seems like something we shouldn’t need to handle. I know this has been asked previously, but I haven’t seen any proper solution.
My current workaround is to generate a UUID
key
at the moment the click (to bring up dialog) is handled, and then pass that
key
to the
getViewModel
call. But this feels very hacky.
a
don't use a viewmodel then? use a simple "factory" instance, and `koinInject`to inject and recreate it
m
But if I don’t use viewmodel then it won’t handle config changes right? Ideally, there would be some mechanism for a
Dialog
to have its own
ViewModelStore
1