Android Development Android Development 2 — Questions and Answers
Question 1: Which lifecycle callback is invoked when an Activity is partially obscured by a dialog-themed Activity but still visible?
- onDestroy()
- onStop()
- onSaveInstanceState() only
- onPause() (Correct answer)
Correct answer: onPause()
onPause() fires when the Activity loses focus but remains partially visible, while onStop() only fires when fully hidden.
Question 2: In Jetpack Compose, what happens when a State<T> object read inside a composable changes its value?
- Nothing until the next configuration change
- The composable must be manually invalidated
- The composable that reads it is scheduled for recomposition (Correct answer)
- The entire Activity is recreated
Correct answer: The composable that reads it is scheduled for recomposition
Compose tracks state reads and automatically recomposes only the composables that read the changed state.
Question 3: A developer needs to run a task that must complete even if the user closes the app, such as uploading logs nightly. Which API is the recommended choice?
- Thread with a while loop
- Handler.postDelayed()
- AsyncTask
- WorkManager (Correct answer)
Correct answer: WorkManager
WorkManager guarantees deferrable, persistent background work that survives app exits and device restarts.
Question 4: What is the primary purpose of the AndroidManifest.xml file?
- Storing user preferences at runtime
- Defining Gradle build dependencies
- Declaring app components, permissions, and metadata to the system (Correct answer)
- Holding string translations
Correct answer: Declaring app components, permissions, and metadata to the system
The manifest declares Activities, Services, permissions, and other essential app information to the Android system.
Question 5: Which storage option is most appropriate for saving a small set of key-value user settings, such as a dark-mode flag?
- Room database with a settings table
- External storage text file
- ContentProvider
- DataStore (or SharedPreferences) (Correct answer)
Correct answer: DataStore (or SharedPreferences)
DataStore, the modern replacement for SharedPreferences, is designed for small key-value or typed settings data.
Question 6: What does the 'lateinit' modifier in Kotlin allow in an Android class?
- Making a property thread-safe
- Allowing a val to be reassigned once
- Lazily computing a val on first access
- Declaring a non-null var that is initialized after construction, e.g. in onCreate() (Correct answer)
Correct answer: Declaring a non-null var that is initialized after construction, e.g. in onCreate()
lateinit defers initialization of a non-null var, commonly used for views or dependencies set up in onCreate().
Question 7: Which component should be used to expose an app's data to other applications in a secure, structured way?
- Service
- BroadcastReceiver
- ContentProvider (Correct answer)
- Fragment
Correct answer: ContentProvider
ContentProvider is the standard component for sharing structured data across app boundaries with permission control.
Which lifecycle callback is invoked when an Activity is partially obscured by a dialog-themed Activity but still visible?