Kotlin, Jetpack Compose, lifecycle, coroutines, architecture
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start freeKotlin null safety means that the type system differentiates between nullable and non-nullable types. This helps catch potential null pointer exceptions at compile time, making apps more robust and reducing runtime crashes.
A data class in Kotlin is declared with the data keyword and is used for classes that primarily hold data. It automatically generates useful methods like equals, hashCode, toString, and copy, making code more concise and readable.
An extension function lets you add new functions to existing classes without modifying their source code. For example, fun String.isEmail() = contains("@") adds an isEmail method to the String class.
onSaveInstanceState method in an Activity?onSaveInstanceState is used to save UI state before an Activity is destroyed, such as during a configuration change. The saved data can be restored in onCreate or onRestoreInstanceState to provide a seamless user experience.
Jetpack Compose uses state holders like remember and mutableStateOf to manage UI state. When the state changes, Compose automatically triggers recomposition, updating only the affected UI elements efficiently.
viewModelScope and lifecycleScope.viewModelScope is tied to the ViewModel's lifecycle and is used for launching coroutines that should survive configuration changes. lifecycleScope is tied to the lifecycle of an Activity or Fragment, and is suitable for UI-related coroutines that should be canceled when the lifecycle ends.
A Flow is a cold, asynchronous data stream that supports operators like map, filter, and can be collected in coroutines. Unlike LiveData, Flow is not tied to the Android lifecycle and is more flexible for complex data transformations and background operations.
Hilt simplifies dependency injection by automatically providing dependencies to Android components using annotations. It reduces boilerplate code, manages component lifecycles, and integrates well with ViewModels, Activities, and Fragments.
Room provides an abstraction over SQLite, allowing you to define entities and DAOs for type-safe database access. You annotate data classes with @Entity and define queries in DAO interfaces, and Room generates the necessary code for CRUD operations.
WorkManager is an API for scheduling deferrable, guaranteed background work that should run even if the app is killed or the device restarts. It's ideal for tasks like syncing data or uploading logs that must eventually complete.
I use coroutines or background threads to offload heavy operations like network or database access. For example, I launch coroutines with Dispatchers.IO for I/O tasks to keep the UI responsive and avoid ANRs.
I use runBlockingTest or TestCoroutineDispatcher from kotlinx-coroutines-test to control coroutine execution in tests. For Flow, I collect emissions in tests and assert expected values, and I use dependency injection to provide mock repositories.