Kotlin
225 bites tagged Kotlin — interview questions with model answers, and 60-second explainers.
WorkManager: Guaranteed, Deferrable Background Work
WorkManager is Android's smart to-do list for background tasks. It guarantees execution for deferrable work, like syncing data, even if the app closes or the device reboots. The footgun is using it for immediate tasks; it prioritizes system health over speed.
Android Foreground Services: Work the User Can See
A foreground service tells Android, "Don't kill this process; the user knows it's running." It's a contract for long-running work made visible by a persistent notification, used for music playback or navigation.
ViewModel: Surviving Android Configuration Changes
A ViewModel acts like a durable backpack for your UI's data, surviving the destruction and recreation of an Activity during a screen rotation. It holds UI state, like user input or fetched data, preventing data loss.
LiveData: Lifecycle-Aware Data Observation
LiveData is a data holder that only notifies active UI components. It connects your ViewModel's data to your UI, preventing crashes and memory leaks by respecting the Android lifecycle.
Android ViewModel: Survive Screen Rotations
A ViewModel is a lifecycle-aware data holder that separates your UI's state from its controller. It keeps data alive during configuration changes like screen rotations, preventing data loss and repeated network calls.
Broadcast Receiver: Responding to System-Wide Events
A Broadcast Receiver is your app's antenna for system-wide announcements. It listens for events like network changes or low battery. The main footgun is performing long tasks here; the system will kill it. Offload work to WorkManager.
Android's Activity Lifecycle: A Screen's Journey
Think of an Activity's lifecycle as a stage play's script. Methods like onCreate() and onPause() are cues for your app screen to set up, appear, or hide. This manages state during interruptions like phone calls.
Android Intents: The App's Messaging System
An Intent is a message requesting an action from another app component. It's the 'glue' that lets you start new screens, launch background services, or ask other apps to open a URL.
Android Activity: A Single Screen in Your App
An Android Activity is the window for a single screen in your app where users interact with your UI. The OS manages a "back stack" of Activities; starting a new screen pushes one on, and pressing back pops it off.
Android Build Variants: One Codebase, Many Apps
Think of build variants as a matrix for your app builds, combining build types (like debug/release) with product flavors (like free/paid). This lets you manage different API endpoints or features for various versions from a single codebase.
Android App Development: Core Languages and Tools
Android apps are primarily built with Kotlin, Java, and C++ using the official SDK. While other languages are possible, they often need 'glue' code to work and may have restricted API access. The main footgun is assuming any language has first-class support.
AndroidManifest.xml: The Blueprint for Your App
AndroidManifest.xml is the blueprint for your app, telling the OS its components, permissions, and requirements before running any code. A missing declaration here is a common source of runtime crashes, as the OS simply won't see your component.
Android Studio: The Official Workshop for Android Apps
Think of Android Studio as the all-in-one workshop for building Android apps. It bundles a code editor, build system, and emulators to write, test, and package software for any Android device. The footgun is underestimating its resource needs.
Kotlin's Type-Safe Builders: Code as Data
Type-safe builders use Kotlin code to create a custom language (DSL) for building complex objects. It's like writing HTML, but the compiler validates your structure. This is common for UI layouts or server configs. The footgun is omitting `@DslMarker`.
Reified Type Parameters: Accessing Generic Types at Runtime
Reified type parameters let you access generic types at runtime, bypassing JVM type erasure. In Kotlin, you use this inside `inline` functions to check types (`obj is T`) or get a class reference. The footgun: `reified` requires `inline` to work.
Kotlin's `in` and `out`: Declaration-Site Variance
Kotlin's `out` and `in` keywords define a generic class as a producer or consumer at its declaration, avoiding Java's repetitive wildcards. Use `out T` for types only returned (produced) and `in T` for types only consumed.
SharedFlow: A Hot Flow for Broadcasting Events
A SharedFlow is a hot stream that broadcasts values to all subscribers, like a live TV channel. It's ideal for one-to-many events, like UI updates or notifications. The main footgun: it never completes, so `collect` will suspend forever.
Kotlin Delegated Properties: Reusing Getter/Setter Logic
Kotlin's delegated properties let you outsource a property's getter/setter logic. Instead of writing boilerplate, you reuse common patterns like lazy initialization (`by lazy`) or observing changes (`by observable`).
StateFlow: A Hot Flow for UI State
StateFlow is a state-holder observable that emits the current and new state updates to its collectors. Use it to expose UI state from a ViewModel to a UI. The footgun: slow collectors can miss intermediate state updates, receiving only the most recent value.
Kotlin Coroutines: async/await for Parallel Results
async starts a coroutine for a parallel result, returning a `Deferred` value. `await` pauses until that result is ready. Use it to run independent network calls concurrently. The footgun: using it for sequential tasks creates a needless race condition.
Structured Concurrency in Kotlin
Structured concurrency treats async operations like code blocks: a parent task's lifetime contains its children. In Kotlin, a `CoroutineScope` ensures if the parent is cancelled, all its child coroutines are too.
Kotlin Flow: Asynchronous Data Streams
A Kotlin Flow is like an async Sequence, emitting multiple values over time without blocking. It's used for live data from a database or streaming network responses. The footgun: Flows are cold; the code doesn't run until a collector calls `.collect()`.
CoroutineScope: The Parent of Your Coroutines
A CoroutineScope acts as a parent to a group of coroutines, managing their lifecycles. When the parent scope is cancelled, all its children are cancelled too. It's used with builders like `launch` to group related work, like in an Android ViewModel.
Coroutine Dispatchers: Telling Your Coroutines Which Thread to Use
A Dispatcher tells a coroutine which thread or thread pool to use for its work. Use `Dispatchers.Default` for CPU-intensive tasks. If none is specified, it inherits from its parent. The main footgun: `Unconfined` can resume on an unexpected thread.
Get Kotlin bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.