Bài viết ngắn gọn, thực hành về xử lý UI events trong Jetpack Compose.
Hệ thống cũ dùng OnClickListeners và interfaces khác. Compose tận dụng tối đa Sealed Classes, Function Types và Lambda Expressions của Kotlin.
Nếu không biết composable là gì, đọc bài giải thích fundamentals.
Mô Hình Hóa UI Events Với Sealed Class
Trước tiên, hiểu UI Events và cách mô hình hóa với Sealed Classes.
Đã mô tả quy trình này cho Java/Kotlin (hệ thống view cũ), nên giữ ngắn.
Quy Trình
Với mỗi màn hình hoặc sub-screen UI, tự hỏi: User có thể tương tác những cách nào?
Ví dụ từ app đầu tiên full Compose, Graph Sudoku:

Sealed class biểu diễn UI interactions màn hình này:
sealed class ActiveGameEvent { data class OnInput(val input: Int) : ActiveGameEvent() data class OnTileFocused(val x: Int, val y: Int) : ActiveGameEvent() object OnNewGameClicked : ActiveGameEvent() object OnStart : ActiveGameEvent() object OnStop : ActiveGameEvent() }Giải thích:
- OnInput: user chạm nút input (0,1,2,3,4)
- OnTileFocused: user chọn ô (ô highlight amber)
- OnNewGameClicked: tự giải thích
- OnStart/OnStop: lifecycle events, composables không quan tâm, nhưng dùng trong Activity làm Container cho composables
Sau khi có sealed class, xử lý nhiều events bằng single event handler function. Đôi khi multiple handler functions hơn, nhớ adapt approach theo yêu cầu dự án.
Kết Nối Kiến Trúc Phần Mềm
Handler events là gì tùy bạn. Ai nghĩ MVVM là gold standard, nhưng nhiều người nhận ra không có single architecture tốt cho mọi tình huống.
Android với Compose, approach hiện tại: minimalist 3rd party, thường có:
- Logic class (Presentation) làm event handler
- ViewModel lưu data render View
- Activity làm Container (không phải god object)
- Composables tạo View

Không quan tâm bạn dùng gì miễn apply separation of concerns. Architecture này đến từ việc hỏi gì nên/kho gộp cùng class.
Dù ViewModel, Fragment, hay Activity làm event handler, setup giống nhau: Function Types!
Trong class chọn, setup event handler function nhận sealed class làm argument:
class ActiveGameLogic( private val container: ActiveGameContainer?, private val viewModel: ActiveGameViewModel, private val gameRepo: IGameRepository, private val statsRepo: IStatisticsRepository, dispatcher: DispatcherProvider ) : BaseLogic<ActiveGameEvent>(dispatcher), CoroutineScope { //... override fun onEvent(event: ActiveGameEvent) { when (event) { is ActiveGameEvent.OnInput -> onInput( event.input, viewModel.timerState ) ActiveGameEvent.OnNewGameClicked -> onNewGameClicked() ActiveGameEvent.OnStart -> onStart() ActiveGameEvent.OnStop -> onStop() is ActiveGameEvent.OnTileFocused -> onTileFocused(event.x, event.y) } } //... }Approach rất organized, dễ test mọi Unit trong class không thư viện 3rd party qua single entry point.
Chưa xong. Cần cách truyền reference hàm onEvent tới Composables. Dùng function reference:
class ActiveGameActivity : AppCompatActivity(), ActiveGameContainer { private lateinit var logic: ActiveGameLogic override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val viewModel = ActiveGameViewModel() setContent { ActiveGameScreen( onEventHandler = logic::onEvent, viewModel ) } logic = buildActiveGameLogic(this, viewModel, applicationContext) } //... }Có người thắc tại sao dùng Activity. Hỏi mình lúc livestream Q&A để trả lời chi tiết.
Tóm lại, Fragments hơi vô nghĩa với Compose theo approach này (không dùng Jetpack Navigation), không sai dùng Activities làm container feature-specific. Chỉ tránh viết god activities.
Cụ thể, cách reference function Kotlin: cung cấp class/interface name (hoặc bỏ qua nếu top-level function), theo sau hai dấu hai chấm, và tên function không arguments/brackets:
onEventHandler = logic::onEventThay OnClickListener Bằng Jetpack Compose onClick Modifier
Với chuẩn bị xong, xem cách hoạt động trong composable. Root composable cần event handler function as parameter:
@Composable fun ActiveGameScreen( onEventHandler: (ActiveGameEvent) -> Unit, viewModel: ActiveGameViewModel ) { //... }Function type syntax hơi tricky, nhưng hiểu đây thực sự là reference đến function, không khác reference đến class.
Không xây god objects, cũng không xây giant composables:
- Chia UI thành phần nhỏ nhất hợp lý
- Wrap trong composable function
- Mỗi composable có UI interaction, phải cho reference event handler function
Composable đại diện nút bấm input Sudoku, nhận event handler by reference:
@Composable fun SudokuInputButton( onEventHandler: (ActiveGameEvent) -> Unit, number: Int ) { Button( onClick = { onEventHandler.invoke(ActiveGameEvent.OnInput(number)) }, modifier = Modifier .requiredSize(56.dp) .padding(2.dp) ) { Text( text = number.toString(), style = inputButton.copy(color = MaterialTheme.colors.onPrimary), modifier = Modifier.fillMaxSize() ) } }Để pass event tới logic class, dùng function invoke, nhận arguments theo function type definition (nhận ActiveGameEvent ở đây).
Đến đây, sẵn sàng handle UI interaction events trong Kotlin (Compose hay không) bằng cách tận dụng ngôn ngữ hiện đại, đẹp này.
Nếu thích bài, share social media và check resources dưới để support independent programmer/content creator.
Social
Tìm mình trên Instagram và Twitter.
Tutorials & Courses
https://youtube.com/wiseass https://www.freecodecamp.org/news/author/ryan-michael-kay/ https://skl.sh/35IdKsj (introduction to Android with Android Studio)