adrielcafe / voyager

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

ScreenModel.onDispose is not being called when exiting application #291

Open rackaracka123 opened 5 months ago

rackaracka123 commented 5 months ago

Consider this code:

class SM : ScreenModel {
    override fun onDispose() {
        println("Disposed")
    }
}
class S : Screen {
    @Composable
    override fun Content() {
         val screenModel = rememberScreenModel { SM() }
    }
}

On disposed is only called when Content leaves the composition and not when the application is destroyed. It would be great if onDisposed or maybe onDestroy would be called to guarantee resources are cleared.

DevSrSouza commented 5 months ago

The disposable of ScreenModel, Screens and Navigator is based on the RememberObserver API. In the case of the application is destroyed, only if Compose it self and the hole composition is not destroyed.

This is the same case for any DisposableEffect. I don't see how we can improve here because we are already using Compose APIs for free resources and listen to lifecycle of the composition.