Activity状态改变

Activity State Changes

Activity状态改变

Different events, some user-triggered and some system-triggered, can cause an Activity to transition from one state to another. This document describes some common cases in which such transitions happen, and how to handle those transitions.

由用户和系统触发的一些不同的事件,能够导致Activity从一个状态向另一个状态转变。本文档描述了这种转换发生的一些常见情况,以及如何处理这些转换。

For more information about activity states, see The Activity Lifecycle.

更多有关Activity状态的信息,请参见The Activity Lifecycle

Configuration change occurs

配置发生变化

There are a number of events that can trigger a configuration change. Perhaps the most prominent example is a change between portrait and landscape orientations. Other cases that can cause configuration changes include changes to language or input device.

有许多事件可以触发配置更改。也许最鲜明的例子是横竖屏转变。可能导致配置更改的其他情况包括对语言或输入设备的更改。

When a configuration change occurs, the activity is destroyed and recreated. To preserve simple transient-state data for the activity, you must override the onSaveInstanceState() method to save the data, and then use onCreate() or onRestoreInstanceState() callbacks to recreate the instance state.

当配置发生变化时,Activity会被销毁并且会被重建。为了在Activity中保存简单的瞬态数据,你必须重写onSaveInstanceState()方法去保存数据,并且之后使用onCreate()onRestoreInstanceState()回调去重建实例状态。

For specific detail about persisting simple data across configuration changes, see the Saving and restoring activity state section of The Activity Lifecycle. For more information about approaches to persisting both simple and complex UI data, see Saving UI States.

有关通过配置改变保存简单数据的具体描述,请参见The Activity LifecycleSaving and restoring activity state部分。有关保存简单和复杂UI数据的方法的更多信息,请参见Saving UI States.

Handling multi-window cases

多窗口处理

When an app enters multi-window mode, available in Android 7.0 (API level 24)and higher, the system notifies the currently running activity of a configuration change, thus going through the lifecycle transitions described above. This behavior also occurs if an app already in multi-window mode gets resized. Your activity can handle the configuration change itself, or it can allow the system to destroy the activity and recreate it with the new dimensions.

当app进入多窗口模式时,在Android 7.0或者更高版本中,系统通知配置改变正在运行的Activity,来完成上面所描述的上命周期转变。如果app处于多窗口模式去调整大小时也会发生这种行为。Activity能够处理它自己的配置变化,或者它允许系统去销毁Activity并且用新的尺寸重建它。

For more information about the multi-window lifecycle, see the Multi-Window Lifecycle section of the Multi-Window Support page.

更多关于多窗口下生命周期的信息,请参见 Multi-Window Support页面下的Multi-Window Lifecycle部分。

In multi-window mode, although there are two apps that are visible to the user, only the one with which the user is interacting is in the foreground and has focus. That activity is in the Resumed state, while the app in the other window is in the Paused state.

在多窗口模式下,尽管有两个app对用户可见,但是仅仅只有一个在前台与用户交互并且拥有焦点。当另一个窗口的app处于暂停状态时,这个Activity处于重新恢复状态。

When the user switches from app A to app B, the system calls onPause() on app A, and onResume() on app B. It switches between these two methods each time the user toggles between apps.

当用户转换app A到app B时,系统会调用app A的onPause()方法和app B的onResume()方法。用户在每次切换app时系统会转换这两种方法。

For more detail about multi-windowing, refer to Multi-Window Support.

更多有关多窗口模式的详细信息,请参考Multi-Window Support

Activity or dialog appears in foreground

Activity或者对话框在前台显示

If a new activity or dialog appears in the foreground, taking focus and partially covering the activity in progress, the covered activity loses focus and enters the Paused state. Then, the system calls onResume() on it.

如果一个新的Activity或者对话框在前台显示,获取焦点并部分覆盖正在运行中的Activity,被覆盖的Activity失去焦点并进入暂停状态。然后,系统会调用它的onResume()方法。

When the covered activity returns to the foreground and regains focus, it calls onResume().

当被覆盖的Activity返回到前台并且重新获取焦点时,它会调用onResume()方法。

If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state. The system then, in rapid succession, calls onPause() and onStop().

如果一个新的Activity或者对话框在前台显示,获取焦点并且完全覆盖正在运行中的Activity,被覆盖的Activity失去焦点并进入到停止状态,系统之后会快速连续的调用onPause()onStop()方法。

When the same instance of the covered activity comes back to the foreground, the system calls onRestart()onStart(), and onResume() on the activity. If it is a new instance of the covered activity that comes to the background, the system does not call onRestart(), only calling onStart() and onResume().

当被覆盖的Activity的相同实例返回至前台时,系统会调用该Activity的onRestart()onStart()和onResume()方法。如果被覆盖的Activity的新实例返回至前台时,系统不会调用onRestart(),仅仅会调用onStart()onResume()方法。

Note: When the user taps the Overview or Home button, the system behaves as if the current activity has been completely covered.

注意:当用户点击概览或home按钮时,系统的行为就好像当前的活动被完全覆盖了一样。

User taps Back button

用户点击返回按钮


If an activity is in the foreground, and the user taps the Back button, the activity transitions through the onPause()onStop(), and onDestroy() callbacks. In addition to being destroyed, the activity is also removed from the back stack.

如果一个Activity处于前台并且用户点击了返回按钮,该Activity会经过onPause()onStop(), and onDestroy()回调函数,除了被销毁之外,该Activity也会被从返回栈移除。

It is important to note that, by default, the onSaveInstanceState() callback does not fire in this case. This behavior is based on the assumption that the user tapped the Back button with no expectation of returning to the same instance of the activity. However, you can override the onBackPressed()method to implement some custom behavior, for example a “confirm-quit" dialog.

在默认情况下,需要值得注意的是,onSaveInstanceState()回调方法不会在这种情况下被调用。这种行为基于用户点击返回键后不期望返回到Activity的相同实例这一假设。然而,你可以重写onBackPressed()方法去实现一些自定义行为,例如“确认退出”对话框。

If you override the onBackPressed() method, we still highly recommend that you invoke super.onBackPressed() from your overridden method. Otherwise the Back button behavior may be jarring to the user.

如果你重写了onBackPressed()方法,我们仍然强烈建议在你的重写方法中调用super.onBackPressed()方法。否则返回按钮的行为可能会与用户造成冲突。



猜你喜欢

转载自blog.csdn.net/agoddog/article/details/79848370
今日推荐