[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Activity state changes\n\nDifferent events, some user-triggered and some system-triggered, can cause an\n[`Activity`](/reference/android/app/Activity) to transition from one state to\nanother. This document describes some common cases in which such transitions\nhappen and how to handle those transitions.\n\nFor more information about activity states, see [The activity\nlifecycle](/guide/components/activities/activity-lifecycle). To learn about how\nthe [`ViewModel`](/reference/androidx/lifecycle/ViewModel) class can\nhelp you manage the activity lifecycle, see the [ViewModel\noverview](/topic/libraries/architecture/viewmodel).\n\nConfiguration change occurs\n---------------------------\n\nThere are a number of events that can trigger a configuration change. Perhaps\nthe most prominent example is a change between portrait and landscape\norientations. Other cases that can cause configuration changes include changes\nto language settings or input device.\n\nWhen a configuration change occurs, the activity is destroyed and recreated. This\ntriggers the following callbacks in the original activity instance:\n\n1. [`onPause()`](/reference/android/app/Activity#onPause())\n2. [`onStop()`](/reference/android/app/Activity#onStop())\n3. [`onDestroy()`](/reference/android/app/Activity#onDestroy())\n\nA new instance of the activity is created, and the following callbacks are triggered:\n\n1. [`onCreate()`](/reference/android/app/Activity#onCreate(android.os.Bundle))\n2. [`onStart()`](/reference/android/app/Activity#onStart())\n3. [`onResume()`](/reference/android/app/Activity#onResume())\n\nUse a combination of `ViewModel` instances, the `onSaveInstanceState()` method, or\npersistent local storage to preserve an activity's UI state across configuration\nchanges. Deciding how to combine these options depends on the complexity of\nyour UI data, use cases for your app, and consideration of the speed of retrieval\nversus memory usage. For more information about saving your activity UI state, see\n[Save UI states](/topic/libraries/architecture/saving-states).\n\n### Handle multi-window cases\n\nWhen an app enters multi-window mode, available in Android 7.0 (API level 24)and\nhigher, the system notifies the running activity of a configuration\nchange, thus going through the lifecycle transitions described previously.\n\nThis\nbehavior also occurs if an app already in multi-window mode gets resized. Your\nactivity can handle the configuration change itself, or it can allow the system\nto destroy the activity and recreate it with the new dimensions.\n\nFor more information about the multi-window lifecycle, see the explanation of\nthe [multi-window\nlifecycle](/guide/topics/ui/multi-window#lifecycle) in the\n[Multi-window support](/guide/topics/ui/multi-window) page.\n\nIn multi-window mode, although there are two apps that are visible to the\nuser, only the one the user is interacting with is in the foreground\nand has focus. That activity is in the Resumed state, while the app in the\nother window is in the Paused state.\n\nWhen the user switches from app A to app B, the system calls\n[`onPause()`](/reference/android/app/Activity#onPause()) on app A and\n[`onResume()`](/reference/android/app/Activity#onResume()) on app B. It switches\nbetween these two methods each time the user toggles between apps.\n\nFor more details about multi-window mode, refer to [Multi-window\nsupport](/guide/topics/ui/multi-window).\n\nActivity or dialog appears in foreground\n----------------------------------------\n\nIf a new activity or dialog appears in the foreground, taking focus and\npartially covering the activity in progress, the covered activity loses focus\nand enters the Paused state. Then, the system calls\n[`onPause()`](/reference/android/app/Activity#onPause()) on it.\n\nWhen the covered activity returns to the foreground and regains focus, the\nsystem calls [`onResume()`](/reference/android/app/Activity#onResume()).\n\nIf a new activity or dialog appears in the foreground, taking focus and\ncompletely covering the activity in progress, the covered activity loses focus\nand enters the Stopped state. The system then, in rapid succession, calls\n`onPause()` and [`onStop()`](/reference/android/app/Activity#onStop()).\n\nWhen the same instance of the covered activity returns to the foreground, the\nsystem calls [`onRestart()`](/reference/android/app/Activity#onRestart()),\n[`onStart()`](/reference/android/app/Activity#onStart()), and\n`onResume()` on the activity. If\nit is a new instance of the covered activity that comes to the background, the\nsystem does not call `onRestart()`, only\n`onStart()` and `onResume()`.\n| **Note:** When the user taps the Overview or Home button, the system behaves as if the current activity has been completely covered.\n\nUser taps or gestures Back\n--------------------------\n\nIf an activity is in the foreground and the user taps or gestures Back,\nthe activity transitions through the\n[`onPause()`](/reference/android/app/Activity#onPause()),\n[`onStop()`](/reference/android/app/Activity#onStop()), and\n[`onDestroy()`](/reference/android/app/Activity#onDestroy())\ncallbacks. The activity is destroyed and removed from the back stack.\n| **Note:** If the activity is a root launcher activity, the system handles the event differently depending on the version of Android that the device is running. For more information, see [Back tap behavior for root launcher\n| activities](/guide/components/activities/tasks-and-back-stack#back-press-behavior).\n\nBy default, the\n[`onSaveInstanceState()`](/reference/android/app/Activity#onSaveInstanceState(android.os.Bundle))\ncallback does not fire in this case. This behavior assumes the user taps Back with\nno expectation of returning to the same instance of the activity.\n\nHowever, you can override the\n[`onBackPressed()`](/reference/android/app/Activity#onBackPressed()) method to\nimplement custom behavior, such as displaying a dialog that asks the user to\nconfirm that they want to exit your app.\n\nIf you override the `onBackPressed()` method, we\nhighly recommend that you still invoke `super.onBackPressed()` from\nyour overridden method. Otherwise the system Back behavior might be jarring to the\nuser.\n\nSystem kills app process\n------------------------\n\nIf an app is in the background and the system needs to free up memory\nfor a foreground app, the system can kill the background app.\nWhen the system kills an app, there is no guarantee that `onDestroy()` is called\nin the app.\n\nTo learn more about how the system decides which processes to\ndestroy, read [Activity state and ejection from\nmemory](/guide/components/activities/activity-lifecycle#asem) and [Processes and\napp lifecycle](/guide/components/activities/process-lifecycle).\n\nTo learn how to save your activity UI state when the system kills your app\nprocess, see [Saving and restoring transient UI\nstate](/guide/components/activities/activity-lifecycle#saras)."]]