{"dataset":{"id":"00000000-0000-0000-0000-000000000057","slug":"android-kotlin-best-practices","name":"Android Kotlin best practices","description":"Source-linked guidance on Android Kotlin fundamentals: Kotlin idioms, coroutines and flows, Jetpack Compose basics, architecture and ViewModels, and testing. Scoped to current Android Kotlin docs.","visibility":"public","version":1,"documentCount":5,"publisher":"Koskamo editorial","license":"Original summaries; linked sources retain their own terms.","reviewedAt":"2026-10-04T00:00:00.000Z","readOnly":true,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},"total":5,"documents":[{"title":"Fundamentals of testing Android apps","sourceUrl":"https://developer.android.com/training/testing/fundamentals","text":"Testable architecture improves readability, maintainability, scalability, and reusability; untestable designs produce bigger, slower, more flaky tests and fewer opportunities for testing different scenarios. By scope, unit tests verify a small portion such as a method or class, end-to-end tests verify larger parts like a screen or user flow, and medium tests check integration between units. Instrumented tests run on a physical or emulated Android device, installed alongside a test app that injects commands and reads state; they're usually UI tests. Local (host-side) tests execute on your development machine or a server, small and fast, isolating the subject under test. Espresso UI tests use onView with perform click and check matches isDisplayed; Compose uses composeTestRule onNodeWithText performClick and assertIsDisplayed. Scope: Android Kotlin. Reviewed October 4, 2026.","id":"android-kotlin-best-practices:Fundamentals of testing Android apps","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"ViewModel and app architecture","sourceUrl":"https://developer.android.com/topic/libraries/architecture/viewmodel","text":"Use ViewModels as implementation details of a screen-level state holder, not for reusable UI components like chip groups or forms, which would share an instance under the same ViewModelStoreOwner. A ViewModel remains in memory until its ViewModelStoreOwner disappears: an activity finishes, a Navigation entry leaves the back stack, or a composable exits the Composition. Request a ViewModel when the system first calls onCreate(); it persists UI state through configuration changes like screen rotation until the activity is finished and destroyed. Override onCleared to clean up dependencies when the ViewModelStoreOwner destroys it. Use viewModelScope, a built-in CoroutineScope following the ViewModel's lifecycle, for business-related operations. From lifecycle version 2.5, pass Closeable objects to the constructor for automatic closing on clearance. Scope: Android Kotlin. Reviewed October 4, 2026.","id":"android-kotlin-best-practices:ViewModel and app architecture","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Jetpack Compose basics","sourceUrl":"https://developer.android.com/jetpack/compose/tutorial","text":"Jetpack Compose simplifies and accelerates Android UI development with less code and intuitive Kotlin APIs, building UI with declarative functions instead of XML layouts. Add the @Composable annotation to make a function composable; composable functions can only be called from other composable functions, and the setContent block defines the activity's layout where they are called. A Kotlin compiler plugin transforms composable functions into the app's UI elements. Compose is built to support Material Design principles; wrap composables in the project's theme (found in the ui.theme subpackage) and Surface to style them. The tutorial uses dynamic theming colors defined by device preferences; set dynamicColor to false in MaterialTheme.kt to change this. Style Text with MaterialTheme.colorScheme.secondary and border Images via Modifier.border with colors and shapes. Scope: Android Kotlin. Reviewed October 4, 2026.","id":"android-kotlin-best-practices:Jetpack Compose basics","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Kotlin coroutines and flows on Android","sourceUrl":"https://developer.android.com/kotlin/flow","text":"Flows emit multiple values sequentially, as opposed to suspend functions returning a single value; emitted values must be of the same type, and a flow can safely make a network request without blocking the main thread. Streams involve a producer adding data, optional intermediaries modifying values, and a consumer; in Android, repositories typically produce UI data consumed by the UI. Create flows with the flow builder APIs, emitting values via emit. Flows are sequential: the producer suspends until a suspend function like fetchLatestNews returns before emitting. The producer cannot emit from a different CoroutineContext; don't call emit from new coroutines or withContext blocks — use callbackFlow instead. Intermediate operators set up operation chains executed only when values are consumed. Scope: Android Kotlin. Reviewed October 4, 2026.","id":"android-kotlin-best-practices:Kotlin coroutines and flows on Android","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Common Kotlin patterns on Android","sourceUrl":"https://developer.android.com/kotlin/common-patterns","text":"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.","id":"android-kotlin-best-practices:Common Kotlin patterns on Android","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"}]}