I’ve got a problem where my VM gets re-initialized...
# compose
m
I’ve got a problem where my VM gets re-initialized when moving between composable screens. Not quite sure on how to go about fixing it. My VM is initialized by
Koin
as shown below.
Copy code
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module

actual val myScreenModule = module {
    viewModel { MyScreen() }
}
The VM itself extends
import androidx.lifecycle.ViewModel
class. And inside my
NavGraphBuilder.addMainGraph
block I instantiate the screen
Copy code
composable(MyScreen.route) {
        MyScreen(
            viewModel = get(),
        )
    }
Not quite sure how to go about things here: is this expected behaviour and I should store VM state inside or is there some other way that I could keep the state?
c
You should use the
getViewModel()
function. See here.
m
Agh, damn. That solves it 😅
👌 1
Thanks a lot, this gave me a bunch of unnecessary headache 👍