What is the purpose of lifecycle awareness in Android apps?
Lifecycle awareness helps an app respond correctly to changes in its state, like when it starts, stops, or is destroyed. This keeps the app efficient and prevents crashes.
Click to reveal answer
beginner
Name the main lifecycle states of an Android Activity.
The main states are: onCreate (start), onStart (visible), onResume (active), onPause (partially visible), onStop (hidden), and onDestroy (cleanup).
Click to reveal answer
intermediate
What is a LifecycleObserver in Android?
A LifecycleObserver is a class that watches lifecycle events of components like Activities or Fragments and reacts to them, helping organize code and manage resources.
Click to reveal answer
intermediate
Why should you avoid heavy work in onResume() and prefer lifecycle-aware components?
Heavy work in onResume() can slow the app and cause a bad user experience. Lifecycle-aware components help run tasks only when needed and stop them when not visible.
Click to reveal answer
intermediate
How does ViewModel help with lifecycle awareness?
ViewModel holds UI data and survives configuration changes like screen rotations, so data is not lost and the UI stays consistent without restarting tasks.
Click to reveal answer
Which lifecycle method is called when an Activity becomes visible but not yet interactive?
AonCreate()
BonStart()
ConResume()
DonPause()
✗ Incorrect
onStart() is called when the Activity becomes visible to the user but is not yet in the foreground for interaction.
What interface do you implement to create a lifecycle-aware observer in Android?
ALifecycleOwner
BViewModel
CActivityLifecycleCallbacks
DLifecycleObserver
✗ Incorrect
LifecycleObserver is the interface used to create classes that observe lifecycle events.
Which lifecycle method is best for releasing resources when an Activity is no longer visible?
AonStop()
BonPause()
ConDestroy()
DonResume()
✗ Incorrect
onStop() is called when the Activity is no longer visible, making it a good place to release resources.
What happens to a ViewModel when the Activity is destroyed due to a configuration change?
AIt is destroyed immediately
BIt is recreated with the Activity
CIt survives and retains data
DIt causes a memory leak
✗ Incorrect
ViewModel survives configuration changes and keeps UI data intact.
Why is lifecycle awareness important for background tasks?
AAll of the above
BTo prevent tasks from running when the app is stopped
CTo save battery and resources
DTo run tasks only when the app is visible
✗ Incorrect
Lifecycle awareness helps manage background tasks efficiently, saving battery and resources by running tasks only when appropriate.
Explain the main lifecycle states of an Android Activity and what happens in each.
Think about when the Activity is created, visible, interactive, partially visible, hidden, and destroyed.
You got /6 concepts.
Describe how lifecycle-aware components improve app performance and user experience.
Consider how apps behave when users switch apps or rotate screens.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of lifecycle awareness in an Android app?
easy
A. To add animations to UI components
B. To change the app's theme colors dynamically
C. To increase the app's download speed
D. To respond to app state changes like start and stop
Solution
Step 1: Understand lifecycle awareness concept
Lifecycle awareness means the app knows when it starts, stops, or pauses.
Step 2: Identify the main goal
The goal is to respond properly to these state changes to save resources and improve user experience.
Final Answer:
To respond to app state changes like start and stop -> Option D
Quick Check:
Lifecycle awareness = respond to state changes [OK]
Hint: Lifecycle awareness means reacting to app start/stop events [OK]
Common Mistakes:
Confusing lifecycle with UI styling
Thinking it controls app download speed
Assuming it only adds animations
2. Which annotation is used to mark a method that listens to lifecycle events in Android Kotlin?
easy
A. @OnClick
B. @OnLifecycleEvent
C. @Override
D. @Subscribe
Solution
Step 1: Recall lifecycle event annotations
In Android Kotlin, @OnLifecycleEvent marks methods that respond to lifecycle changes.
Step 2: Compare options
@OnClick is for button clicks, @Override is for overriding methods, @Subscribe is for event bus patterns.