Created
December 6, 2024 11:16
-
-
Save Andrew0000/8f0a18d1e8c24f6ee681940b5fe87db1 to your computer and use it in GitHub Desktop.
doOnceSavable(): Invokes the given block once per Composable across configuration changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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