Koskamo

Common Kotlin patterns on Android

Document v1 · published by Koskamo editorial · updated 2026-10-04

Original source

Override methods in Fragments using the override keyword, referencing parent implementations via super. Handle nullable parameters safely, indicated by the question mark suffix. Initialize object properties when declaring the object, but defer View initialization with lateinit because a Fragment's View objects aren't ready to be inflated until onCreateView. Assign these properties in onViewCreated, as accessing an uninitialized property throws UninitializedPropertyAccessException. For click events, Single Abstract Method (SAM) conversion implements OnClickListener cleanly with an anonymous function passed to setOnClickListener. Define type-linked constants in companion objects rather than at the top level of the file. Avoid duplicate code like ViewModel access with Kotlin's property delegation syntax, such as viewModels(), which retrieves a Fragment-scoped ViewModel provided by Android KTX. Scope: Android Kotlin. Reviewed October 4, 2026.

Source and provenance

Original source
developer.android.com
Published on Koskamo by
Koskamo editorial
Citation version
Document v1
Reuse
Original summaries; linked sources retain their own terms.

The original source supplies the underlying information. Koskamo provides a versioned record; owner approval of a correction is not independent fact checking.