Understanding the Android Activity Lifecycle
페이지 정보
본문
The Android Activity lifecycle manages an activity's states during its lifetime. onCreate initializes activity with inflated layout and essential setup. onStart makes activity visible to the user. onResume begins user interaction with the activity. onPause pauses ongoing operations when activity loses focus. Save critical data in onPause as it's guaranteed to execute. onStop stops visible operations when activity is no longer visible. onRestart reinitializes components after being stopped. onDestroy cleans up resources before activity destruction. Configuration changes like rotation recreate activities by default. ViewModel survives configuration changes, preserving UI data. onSaveInstanceState saves transient UI state in a Bundle. onRestoreInstanceState restores saved state after recreation. can occur when system needs resources. ViewModel with SavedStateHandle survives both rotation and process death. Lifecycle-aware components observe lifecycle state changes. LifecycleObserver notifies components of lifecycle events. launchWhenStarted and launchWhenResumed tie coroutines to lifecycle. Understanding the lifecycle is essential for correct app behavior. Test lifecycle handling with configuration change simulations.