AAD AAD Jetpack Libraries & Modern Android Development 1 — Questions and Answers
Question 1: What is the primary purpose of ViewModel in Android Jetpack?
- Inflate XML layouts
- Store and manage UI-related data that survives configuration changes (Correct answer)
- Handle network requests directly
- Replace the Activity class
Correct answer: Store and manage UI-related data that survives configuration changes
ViewModel is designed to store and manage UI-related data in a lifecycle-conscious way so that data survives configuration changes like screen rotations.
Question 2: Which Jetpack component is used to schedule deferrable, guaranteed background work?
- LiveData
- Room
- WorkManager (Correct answer)
- Navigation
Correct answer: WorkManager
WorkManager is the Jetpack library for scheduling deferrable, constraint-aware background tasks that are guaranteed to run even if the app exits or the device restarts.
Question 3: What type does LiveData emit that makes it lifecycle-aware?
- It uses callbacks registered manually
- It only delivers updates to observers in an active lifecycle state (Correct answer)
- It always delivers updates regardless of lifecycle
- It emits data only once on subscription
Correct answer: It only delivers updates to observers in an active lifecycle state
LiveData is lifecycle-aware and only notifies observers that are in an active lifecycle state (STARTED or RESUMED), preventing crashes and memory leaks.
Question 4: In Jetpack Navigation, what is a NavGraph?
- A graph database for storing navigation history
- An XML resource that defines destinations and actions for in-app navigation (Correct answer)
- A Kotlin class that manages the back stack manually
- A UI component that renders breadcrumbs
Correct answer: An XML resource that defines destinations and actions for in-app navigation
A NavGraph is an XML resource file that centralizes all navigation destinations (Fragments, Activities) and the actions connecting them in one place.
Question 5: What annotation marks a method as a database query in a Room DAO?
- @Select
- @Find
- @Query (Correct answer)
- @Fetch
Correct answer: @Query
@Query is the Room annotation used on DAO methods to define custom SQL SELECT (or other) statements to retrieve data from the database.
Question 6: Which class in Jetpack's Data Binding library is automatically generated for each layout file?
- BindingAdapter
- ViewHolder
- A Binding class named after the layout file in PascalCase + 'Binding' (Correct answer)
- DataBinder
Correct answer: A Binding class named after the layout file in PascalCase + 'Binding'
For a layout file named activity_main.xml, Data Binding generates ActivityMainBinding, a class that provides direct references to all bound views and variables.
What is the primary purpose of ViewModel in Android Jetpack?