Created
March 13, 2025 10:06
-
-
Save chiragthummar/b3912b432802bb8fd30923c6384833d9 to your computer and use it in GitHub Desktop.
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
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.DisposableEffect | |
import androidx.lifecycle.Lifecycle | |
import androidx.lifecycle.LifecycleEventObserver | |
import androidx.lifecycle.LifecycleOwner | |
import androidx.lifecycle.compose.LocalLifecycleOwner | |
@Composable | |
fun MyEventListener( | |
lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, | |
onEvent: (LifecycleOwner, Lifecycle.Event) -> Unit | |
) { | |
DisposableEffect(lifecycleOwner) { | |
val observer = LifecycleEventObserver { source, event -> | |
onEvent(source, event) | |
} | |
lifecycleOwner.lifecycle.addObserver(observer) | |
onDispose { | |
lifecycleOwner.lifecycle.removeObserver(observer) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment