Enhance your understanding of Android app lifecycle events and effective state management techniques. This quiz covers basic concepts, essential lifecycle callbacks, memory handling, and strategies for maintaining user state during configuration changes in Android development.
Which Android lifecycle method is called when an activity is first created, such as when a user launches an app?
Explanation: The onCreate() method is called when the activity is initially created and is where you should set up essential components. onResume() is triggered when the activity comes to the foreground but isn't used for initial setup. onDestroy() is called just before the activity is destroyed, not when it is created. onStartt() is a typo; the correct method is onStart(), which is still not the method used for initial creation.
If a device is rotated and your app needs to save a user's text input, which lifecycle method should you use to save this data?
Explanation: onSaveInstanceState() is the correct method for saving small amounts of UI data, like text input, before a configuration change such as rotation. onPause() deals with pausing processes but isn't designed to save UI state. onRestart() is called when the activity is restarting, not when saving state. onRemoveState() is not a valid Android lifecycle method.
Which lifecycle callback is invoked when an activity becomes visible to the user, such as when returning from another app screen?
Explanation: onStart() is called when the activity becomes visible to the user. onPause() is triggered when the activity is partially obscured or not in the foreground. onDestroy() occurs when the activity is finishing or being destroyed by the system. onExecute() is not an Android lifecycle callback.
When the user switches to another app but hasn't closed the current one, which method is called to pause your activity?
Explanation: onPause() is executed when the system is about to start resuming another activity, such as when the user navigates elsewhere. onDestroy() is for final cleanup before an activity is killed. onComplete() and onPrepare() are not standard Android activity lifecycle callbacks.
Which method is always called just before an activity is destroyed, whether due to configuration change or user action?
Explanation: onDestroy() is always called before the activity is destroyed, enabling final resource cleanup. onResume() is related to the activity's transition to the foreground, not to its destruction. onStart() occurs when the activity becomes visible. onDelete() is not an activity lifecycle method.
What is a recommended way to prevent memory leaks when an activity is destroyed in Android?
Explanation: Clearing references to context objects helps prevent memory leaks after activity destruction, freeing up resources. Saving data in onRestart() does not handle memory leaks. Storing data in static variables can cause more leaks since they persist longer. Adding more event listeners may worsen leaks by holding unwanted references.
When a user presses the device's back button to close an activity, which lifecycle method is guaranteed to be called?
Explanation: onDestroy() is called before the activity is fully closed when the back button is pressed. onResume() indicates that the activity is entering the foreground, which doesn't happen during closure. onPause() also occurs, but it is not guaranteed to be the final call. onReboot() does not exist in the activity lifecycle.
Which approach should you use to avoid blocking the main thread during a long database query in an Android activity?
Explanation: Executing long-running operations in a background thread keeps the main thread responsive and prevents app freezing. Directly executing queries in onPause() is not appropriate and may still block the main thread. Storing results in global variables is not efficient and can cause memory issues. Disabling user interaction does not solve the underlying problem of blocking the main thread.
After a device rotation, how can an activity restore the text entered in an EditText field for the user?
Explanation: You should use the Bundle in onRestoreInstanceState() to restore the previous value and re-display it after rotation. Re-initializing the EditText will clear user input. Calling onResume() alone does not restore instance state. Garbage collection deals with memory management, not restoring UI state.
What distinguishes onPause() from onStop() in the Android activity lifecycle?
Explanation: onPause() occurs first when the activity is partially obscured but still visible, while onStop() happens when the activity is no longer visible. onPause() can be called on a still-visible activity, not never. onStop() does not precede onPause(); they happen in that specific order. onPause() does not restart the activity.