Skip to content

Instantly share code, notes, and snippets.

@tfcporciuncula
Last active May 8, 2023 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfcporciuncula/ef080b17eff6a0dba6e639333bf7f587 to your computer and use it in GitHub Desktop.
Save tfcporciuncula/ef080b17eff6a0dba6e639333bf7f587 to your computer and use it in GitHub Desktop.
// In an activity:
private val presenter by retain { entry ->
// entry exposes the viewModelScope, savedStateHandle, and a way to listen to onClear()
MyPresenter()
}
// Or in a composable, implicitly:
val presenter = retain { MyPresenter() } // this will look at LocalViewModelStoreOwner.current
// Or explicitly:
val activity = ...
val presenter = retain (owner = activity) { MyPresenter() }
// In a Compose NavHost:
composable(route = "myRoute") { navBackStackEntry ->
val presenter = retain(navBackStackEntry) { MyPresenter() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment