Android App Lifecycle and State Management Quiz Quiz

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.

  1. App Initialization Stage

    Which Android lifecycle method is called when an activity is first created, such as when a user launches an app?

    1. onResume()
    2. onDestroy()
    3. onStartt()
    4. onCreate()

    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.

  2. Preserving Simple UI State

    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?

    1. onRestart()
    2. onSaveInstanceState()
    3. onRemoveState()
    4. onPause()

    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.

  3. Foreground Visibility

    Which lifecycle callback is invoked when an activity becomes visible to the user, such as when returning from another app screen?

    1. onStart()
    2. onDestroy()
    3. onPause()
    4. onExecute()

    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.

  4. Pausing Activity Execution

    When the user switches to another app but hasn't closed the current one, which method is called to pause your activity?

    1. onPrepare()
    2. onDestroy()
    3. onPause()
    4. onComplete()

    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.

  5. App Termination

    Which method is always called just before an activity is destroyed, whether due to configuration change or user action?

    1. onResume()
    2. onStart()
    3. onDestroy()
    4. onDelete()

    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.

  6. Memory Efficiency

    What is a recommended way to prevent memory leaks when an activity is destroyed in Android?

    1. Clear references to context objects
    2. Add more event listeners
    3. Store all data in static variables
    4. Save extra data in onRestart()

    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.

  7. User Navigation Scenario

    When a user presses the device's back button to close an activity, which lifecycle method is guaranteed to be called?

    1. onReboot()
    2. onResume()
    3. onPause()
    4. onDestroy()

    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.

  8. Handling Long Running Operations

    Which approach should you use to avoid blocking the main thread during a long database query in an Android activity?

    1. Store all database results in global variables
    2. Disable user interaction during the process
    3. Perform the query in a background thread
    4. Execute the query directly in onPause()

    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.

  9. Restoring User State Example

    After a device rotation, how can an activity restore the text entered in an EditText field for the user?

    1. Rely on garbage collection
    2. Retrieve the value from the Bundle in onRestoreInstanceState()
    3. Re-initialize the EditText with a new value
    4. Call onResume() again to refresh data

    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.

  10. Difference Between onPause and onStop

    What distinguishes onPause() from onStop() in the Android activity lifecycle?

    1. onStop() is called before onPause()
    2. onPause() is never called on a visible activity
    3. onPause() restarts the activity
    4. onPause() is called before onStop() when the activity is partially obscured

    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.