Kotlin Idioms for Android
Kotlin smooths over many rough edges of the Android Java APIs. Here are the idioms you will use constantly.
Platform types and null safety
Values returned from Java/Android APIs have platform types — Kotlin does not know whether they are nullable. The compiler lets you treat them as either, but a wrong assumption crashes at runtime:
val extras = intent.extras // platform type Bundle!
val name = extras?.getString("name") // be defensive: treat as nullable
?: "guest"
When calling Android/Java APIs, assume return values can be null unless the docs
or annotations say otherwise. Use ?. and ?: at the boundary.
Scope functions for setup
apply and also make object configuration concise:
val intent = Intent(this, DetailActivity::class.java).apply {
putExtra("id", 42)
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
Concise click and callbacks
Lambdas replace anonymous inner classes:
button.setOnClickListener { viewModel.submit() }
In Compose this is even simpler — onClick = { ... }.
Extension functions for boilerplate
Add helpers to framework types:
fun Context.toast(message: String) =
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
// usage inside an Activity/Context
toast("Saved")
when over sealed UI state
Model screen state as a sealed type and render exhaustively:
sealed interface UiState {
data object Loading : UiState
data class Success(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}
@Composable
fun Screen(state: UiState) = when (state) {
UiState.Loading -> Spinner()
is UiState.Success -> ItemList(state.items)
is UiState.Error -> ErrorBanner(state.message)
}
This pattern — a sealed UiState rendered by an exhaustive when — is the
backbone of the architecture in Part 8.
Exercises
-
Write a
Context.toast(...)extension and use it. -
Model a
UiStatesealed interface withLoading,Success, andError, and write an exhaustivewhenover it.
Previous: UI & Jetpack Compose · Next: Coroutines on Android