Last active
December 9, 2025 02:35
-
-
Save benigumocom/1fc2ba6d5fd21b31b5ff84e0ad0ae3c8 to your computer and use it in GitHub Desktop.
How to handle UI interactions with event processing
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
| // No ViewModel | |
| @Composable | |
| fun UiOnlySample() { | |
| val context = LocalContext.current | |
| Button(onClick = { | |
| Toast.makeText(context, "Clicked!", Toast.LENGTH_SHORT).show() | |
| }) { | |
| Text("Click") | |
| } | |
| } |
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
| // State Management | |
| class StateFlowViewModel : ViewModel() { | |
| private val _state = MutableStateFlow("initial") | |
| val state = _state.asStateFlow() | |
| fun update() { | |
| _state.value = "updated at " + System.currentTimeMillis() | |
| } | |
| } | |
| @Composable | |
| fun StateFlowSample(vm: StateFlowViewModel = viewModel()) { | |
| val value by vm.state.collectAsState() | |
| Column { | |
| Text(value) | |
| Button(onClick = { vm.update() }) { | |
| Text("Update") | |
| } | |
| } | |
| } |
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
| // Strict One-Time + Ordered Delivery | |
| class ChannelViewModel : ViewModel() { | |
| private val _channel = Channel<String>(capacity = Channel.BUFFERED) | |
| val eventFlow = _channel.receiveAsFlow() | |
| fun sendEvent() { | |
| viewModelScope.launch { | |
| _channel.send("Hello from Channel!") | |
| } | |
| } | |
| } | |
| @Composable | |
| fun ChannelSample(vm: ChannelViewModel = viewModel()) { | |
| val context = LocalContext.current | |
| LaunchedEffect(Unit) { | |
| vm.eventFlow.collect { msg -> | |
| Toast.makeText(context, msg, Toast.LENGTH_SHORT).show() | |
| } | |
| } | |
| Button(onClick = { vm.sendEvent() }) { | |
| Text("Send Event") | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UI Only
StateFlow
SharedFlow
replay = 0+extraBufferCapacity = 1helps prevent event loss.Channel
【Android】How to handle UI interactions with event processing
https://android.benigumo.com/20251209/how-to-handle-ui-interactions/