Skip to content

Instantly share code, notes, and snippets.

@Andrew0000
Created December 6, 2024 11:16
Show Gist options
  • Select an option

  • Save Andrew0000/8f0a18d1e8c24f6ee681940b5fe87db1 to your computer and use it in GitHub Desktop.

Select an option

Save Andrew0000/8f0a18d1e8c24f6ee681940b5fe87db1 to your computer and use it in GitHub Desktop.
doOnceSavable(): Invokes the given block once per Composable across configuration changes.
/**
* Invokes the given [block] once.
* Doesn't invoke again after config changes (screen rotation) on Android.
* Helpful for screen appearance logging.
*/
@Composable
fun doOnceSavable(block: () -> Unit) {
val done = rememberSaveable { mutableStateOf(false) }
LaunchedEffect(Unit) {
if (!done.value) {
block()
done.value = true
}
}
}
// Usage:
doOnceSavable {
// eventLogger.logScreenAppearance()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment