Replies: 1 comment
-
|
I ended up with something a bit simpler: @Composable
context(scope: CoroutineScope)
fun <T> retainMolecule(
vararg keys: Any?,
mode: RecompositionMode = RecompositionMode.ContextClock,
context: CoroutineContext = scope.coroutineContext,
body: @Composable () -> T
) = retain(*keys) {
scope.launchMolecule(mode, context, body = body)
}This way I can share the retained coroutine scope across views. context(retain { CoroutineContext(AndroidUiDispatcher.Main) }) {
val stateFlow = retainMolecule(someParameter) { present(someParameter) }
val state by stateFlow.collectAsStateWithLifecycle()
SomeView.Pane(state, ...)
// More views or panes could go here, for example, when using nav3.
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This morning I learned that the retain API has been released as part of the Jetpack Compose December '25 release. I have been using
ViewModels solely to retain a reference to my molecules and I am exploring replacing them withretain. I put together a simple composable function calledretainMolecule:Which I can use as follows in my views:
Is this approach good enough or may I be missing something?
Beta Was this translation helpful? Give feedback.
All reactions