Thread
#compose
    Matti MK

    Matti MK

    10 months ago
    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.
    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
    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?
    Csaba Kozák

    Csaba Kozák

    10 months ago
    You should use the
    getViewModel()
    function. See here.
    Matti MK

    Matti MK

    10 months ago
    Agh, damn. That solves it 😅
    Thanks a lot, this gave me a bunch of unnecessary headache 👍