adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.59k stars 137 forks source link

the android app restart/recreate all over each time I minimize the app #491

Open Tweeel opened 1 month ago

Tweeel commented 1 month ago

I am working on a KMM project and facing an issue with my Android app. The iOS app works perfectly, but the Android app restarts and recreates the whole activity each time I minimize it.

nvkleban commented 1 month ago

Have you enabled "Don't keep activities" in developer settings on your phone?

Tweeel commented 1 month ago

No, and I just found the problem. Initially, I was passing the viewModels directly to the screens. I created a viewModelFactory to avoid the need to pass it each time, to facilitate testing later:

class ViewModelFactory : KoinComponent {
    inline fun <reified T : ScreenModel, reified P> getViewModel(parameter: P): T {
        val viewModel: T by inject { parametersOf(parameter) }
        return viewModel
    }

    inline fun <reified T: ScreenModel> getViewModel(): T {
        val viewModel: T by inject()
        return viewModel
    }
}

I noticed from another issue that removing "then" and calling the viewModel using the getScreenModel function from the screen content should fix the problem, and it actually did in my case. But it would still be nice if we could directly pass the viewModel in the parameter, as I mentioned earlier, to make testing easier.

nvkleban commented 1 month ago

I was passing the viewModels directly to the screens

Haven't worked with Koin, but have you checked documentation? it looks pretty straightforward