Deep Kotlin tutorial covering !!, scope functions, shared mutable state, equality with examples, diagram, mistakes, and interview answers.
Common Kotlin Pitfalls in Android
What is Common Kotlin Pitfalls in Android?
Common Kotlin Pitfalls in Android is an important Kotlin topic from 1.10 Common Kotlin Pitfalls in Android. In this lesson we focus only on the concepts that belong together: !!, scope functions, shared mutable state, equality, copying, closure leaks, public API misuse. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.
Why students should learn it
- Understand Common Kotlin Pitfalls in Android in simple language
- Use !! in real Kotlin or Android code
- Explain Common Kotlin Pitfalls in Android in interviews with examples and trade-offs
How it works
Common Kotlin Pitfalls in Android: !!, scope functions, shared mutable state, equality, copying, closure leaks, public API misuse works by giving asynchronous work a clear owner and execution rule. In real Android code, first identify who owns the work, then choose the dispatcher, then decide how cancellation and errors should behave. This prevents background work from continuing after the screen or ViewModel no longer needs it.
- Identify what problem Common Kotlin Pitfalls in Android: !!, scope functions, shared mutable state, equality, copying, closure leaks, public API misuse solves.
- Write the smallest Kotlin snippet that demonstrates it.
- Add one Android or JVM use case where it matters.
- Compare it with a similar concept so the difference is clear.
- Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.
Types / Classification
- !!: core concept in this lesson
- scope functions: core concept in this lesson
- shared mutable state: core concept in this lesson
- equality: core concept in this lesson
- copying: core concept in this lesson
- closure leaks: core concept in this lesson
- public API misuse: core concept in this lesson
- Scope: owner of coroutine lifetime
Important differences
| Concept 1 | Concept 2 | How to remember it |
|---|---|---|
| suspend | blocking | suspend pauses coroutine; blocking occupies a thread. |
| launch | async | launch returns Job; async returns Deferred result. |
| scope | dispatcher | scope owns lifetime; dispatcher chooses execution thread. |
Diagram
flowchart TD
A["1.10 Common Kotlin Pitfalls in Android"] --> B["Common Kotlin Pitfalls in Android"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]
Examples
ViewModel coroutine
fun refresh() {
viewModelScope.launch {
val data = withContext(Dispatchers.IO) { repository.load() }
_state.value = UiState.Content(data)
}
}
Parallel work
suspend fun loadBoth() = coroutineScope {
val profile = async { api.profile() }
val settings = async { api.settings() }
profile.await() to settings.await()
}
When to use it
Use it when: Apply this directly in Android app code - ViewModels, Fragments/Activities, and Compose screens are where these patterns show up daily.
Avoid it when: Avoid copying a pattern from a plain Kotlin script into Android code without first checking its lifecycle implications.
Common mistakes
- Using GlobalScope for screen work
- Blocking Dispatchers.Main
- Catching Exception and swallowing CancellationException
- Using runBlocking in Android UI code
- Launching work without clear ownership
Interview questions
Read each answer in four parts: simple answer, code example, trade-off, and common mistake.
1. What is Common Kotlin Pitfalls in Android in 1.10 Common Kotlin Pitfalls in Android?
Answer: Common Kotlin Pitfalls in Android is an important Kotlin topic from 1.10 Common Kotlin Pitfalls in Android. In this lesson we focus only on the concepts that belong together: !!, scope functions, shared mutable state, equality, copying, closure leaks, public API misuse. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.
fun refresh() {
viewModelScope.launch {
val data = withContext(Dispatchers.IO) { repository.load() }
_state.value = UiState.Content(data)
}
}- Trade-off: suspend and blocking solve related but different problems: suspend pauses coroutine; blocking occupies a thread.
- Common mistake: Using GlobalScope for screen work
2. How does Common Kotlin Pitfalls in Android work in Kotlin or Android code?
Answer: Common Kotlin Pitfalls in Android: !!, scope functions, shared mutable state, equality, copying, closure leaks, public API misuse works by giving asynchronous work a clear owner and execution rule. In real Android code, first identify who owns the work, then choose the dispatcher, then decide how cancellation and errors should behave. This prevents background work from continuing after the screen or ViewModel no longer needs it.
suspend fun loadBoth() = coroutineScope {
val profile = async { api.profile() }
val settings = async { api.settings() }
profile.await() to settings.await()
}- Trade-off: suspend and blocking solve related but different problems: suspend pauses coroutine; blocking occupies a thread.
- Common mistake: Blocking Dispatchers.Main
3. What is one common mistake with Common Kotlin Pitfalls in Android?
Answer: A common mistake is: Using GlobalScope for screen work Another is: Blocking Dispatchers.Main
fun refresh() {
viewModelScope.launch {
val data = withContext(Dispatchers.IO) { repository.load() }
_state.value = UiState.Content(data)
}
}- Trade-off: suspend and blocking solve related but different problems: suspend pauses coroutine; blocking occupies a thread.
- Common mistake: Using GlobalScope for screen work
4. How would you explain Common Kotlin Pitfalls in Android with a small example?
Answer: Common Kotlin Pitfalls in Android is an important Kotlin topic from 1.10 Common Kotlin Pitfalls in Android. In this lesson we focus only on the concepts that belong together: !!, scope functions, shared mutable state, equality, copying, closure leaks, public API misuse. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.
fun refresh() {
viewModelScope.launch {
val data = withContext(Dispatchers.IO) { repository.load() }
_state.value = UiState.Content(data)
}
}- Trade-off: suspend and blocking solve related but different problems: suspend pauses coroutine; blocking occupies a thread.
- Common mistake: Blocking Dispatchers.Main
5. What is the trade-off of using Common Kotlin Pitfalls in Android?
Answer: suspend and blocking solve related but different problems: suspend pauses coroutine; blocking occupies a thread.
suspend fun loadBoth() = coroutineScope {
val profile = async { api.profile() }
val settings = async { api.settings() }
profile.await() to settings.await()
}- Trade-off: suspend and blocking solve related but different problems: suspend pauses coroutine; blocking occupies a thread.
- Common mistake: Using GlobalScope for screen work
Practice task
Write one Kotlin example for Common Kotlin Pitfalls in Android. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.