Android View-在“活动”中自动保存和恢复的内容 [英] Android View - What is automatically saved and restored in an Activity

查看:116
本文介绍了Android View-在“活动”中自动保存和恢复的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的初学者。

I am a beginner with Android.

在Android中,一些通用元素可以自动保存在 onSaveInstanceState / onRestoreInstanceState

In Android, some generic elements can be automatically saved/restored in onSaveInstanceState/onRestoreInstanceState.

例如, EditText 保存/恢复 Text 属性, RatingBar 保存/恢复 Rating 属性...

For example, an EditText saves/restores the Text property, a RatingBar saves/restores the Rating property...

我从一些测试中看到了,但是我在文档中找不到任何内容。

I see from some tests but I can't find anything about this in the documentation.

我怎么能知道在没有我干预的情况下隐式保存/恢复的内容?

例如,在哪里可以找到 EditText.Text 是否自动保存/恢复?

For example, where I can find that the EditText.Text is automatically saved/restored?

我特别不想测试所有属性。

I specifically don't want test all properties.

从JRG答案中编辑:

https://developer.android.com/guide /components/activities/activity-lifecycle.html


保存活动状态为活动开始停止,系统
调用onSaveInstanceState()方法< ...>此方法的默认实现
保存有关
活动的视图层次结构状态的瞬时信息,例如文本或EditView小部件中的文本或
ListView小部件的滚动位置。

Save your activity state As your activity begins to stop, the system calls the onSaveInstanceState() method<...> The default implementation of this method saves transient information about the state of the activity's view hierarchy, such as the text in an EditText widget or the scroll position of a ListView widget.

我怎么知道默认的实现是什么保存/恢复的时间?

How I can know what is the default implementation of save/restore?

重新读取JRG答案后的第二次编辑:


默认情况下,系统使用Bundle实例状态在您的活动布局中保存有关>每个View对象的信息(例如,在> EditText小部件中输入的文本值)。

By default, the system uses the Bundle instance state to save information about >each View object in your activity layout (such as the text value entered into >an EditText widget).

默认实现保存/恢复元素视图的所有状态。

The default implementation saves/restores all state of element view.

推荐答案

< > Android文档,其中介绍了有关保存状态的内容,以及一篇很好的文章,介绍了如何在活动和片段中保存状态。

Android Documentation that explains about saving states and a very good article on saving states in activity and fragment.

  • Android Documentation: https://developer.android.com/guide/components/activities/activity-lifecycle.html
  • Additional Article: https://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en

保存和恢复活动状态在某些情况下,您的活动由于正常的应用行为而遭到破坏,例如用户按下后退按钮时,或者您的活动通过调用finish()方法发出自己的破坏信号。如果该活动处于已停止状态并且很长时间没有使用,或者前台活动需要更多资源,则系统还可能破坏包含您的活动的进程以恢复内存。

Saving and restoring activity state There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling the finish() method. The system may also destroy the process containing your activity to recover memory if the activity is in the Stopped state and hasn't been used in a long time, or if the foreground activity requires more resources.

当由于用户按下Back或活动完成而破坏活动时,该活动实例的系统概念将永远消失,因为该行为表明不再需要该活动。但是,如果系统由于系统限制(而不是正常的应用行为)破坏了活动,则尽管实际的Activity实例已消失,但系统仍会记住该实例的存在,因此,如果用户导航至该实例,则系统会创建一个新实例。活动的实例,使用一组保存的数据来描述活动被销毁时的状态。系统用来恢复先前状态的已保存数据称为实例状态,是存储在Bundle对象中的键/值对的集合。

When your activity is destroyed because the user presses Back or the activity finishes itself, the system's concept of that Activity instance is gone forever because the behavior indicates the activity is no longer needed. However, if the system destroys the activity due to system constraints (rather than normal app behavior), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed. The saved data that the system uses to restore the previous state is called the instance state and is a collection of key-value pairs stored in a Bundle object.

默认情况下,系统使用Bundle实例状态在活动布局中保存有关每个View对象的信息(例如,输入到EditText小部件中的文本值)。 因此,如果您的活动实例被破坏并重新创建,则布局状态将恢复为之前的状态,而无需您执行任何代码。但是,您的活动可能具有更多要还原的状态信息,例如跟踪用户在活动中进度的成员变量。

By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText widget). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. However, your activity might have more state information that you'd like to restore, such as member variables that track the user's progress in the activity.

保存您的活动状态
当您的活动开始停止时,系统会调用onSaveInstanceState()方法,以便您的活动可以使用一组键值对保存状态信息。此方法的默认实现保存有关活动的视图层次结构状态的瞬时信息,例如EditText小部件中的文本或ListView小部件的滚动位置。您的应用应在onPause()方法之后和onStop()之前实现onSaveInstanceState()回调。不要在onPause()中实现此回调。

Save your activity state As your activity begins to stop, the system calls the onSaveInstanceState() method so your activity can save state information with a collection of key-value pairs. The default implementation of this method saves transient information about the state of the activity's view hierarchy, such as the text in an EditText widget or the scroll position of a ListView widget. Your app should implement the onSaveInstanceState() callback after the onPause() method, and before onStop(). Do not implement this callback in onPause().

警告:必须始终调用onSaveInstanceState()的超类实现,以便默认实现可以保存视图层次结构的状态。

Caution: You must always call the superclass implementation of onSaveInstanceState() so the default implementation can save the state of the view hierarchy.

要保存活动的其他状态信息,必须重写onSaveInstanceState()并将键值对添加到在活动中保存的Bundle对象中被意外破坏。例如:

To save additional state information for your activity, you must override onSaveInstanceState() and add key-value pairs to the Bundle object that is saved in the event that your activity is destroyed unexpectedly. For example:



static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);


    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}




注意:为了使Android系统能够恢复活动中视图的状态,每个视图必须具有由android:id属性提供的唯一ID。

Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.

要保存持久数据,例如用户首选项或数据库的数据,当您的活动处于前台时,您应该抓住适当的机会。如果没有出现这种机会,则应在onStop()方法期间保存此类数据。

To save persistent data, such as user preferences or data for a database, you should take appropriate opportunities when your activity is in the foreground. If no such opportunity arises, you should save such data during the onStop() method.

还原活动状态
在先前销毁活动之后重新创建活动,您可以从系统传递到活动的捆绑包中恢复保存的状态。回调方法onCreate()和onRestoreInstanceStateState()都接收包含实例状态信息的同一个Bundle。

Restore your activity state When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes to your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.

因为无论系统是否在创建,都将调用onCreate()方法一个新的活动实例或重新创建一个先前的活动实例,您必须先检查状态Bundle是否为空,然后再尝试读取它。如果为null,则系统将创建活动的新实例,而不是还原先前被破坏的活动。

Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.

例如,以下代码片段显示如何在onCreate()中恢复某些状态数据:

For example, the following code snippet shows how you can restore some state data in onCreate():



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first


    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}




您可以选择实现onRestoreInstanceState(),而不是在onCreate()期间恢复状态, onStart()方法。系统仅在存在要还原的保存状态时才调用onRestoreInstanceState(),因此您无需检查Bundle是否为空:

Instead of restoring the state during onCreate() you may choose to implement onRestoreInstanceState(), which the system calls after the onStart() method. The system calls onRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is null:



public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);


    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}




警告:始终调用onRestoreInstanceState的超类实现(),因此默认实现可以恢复视图层次结构的状态。

Caution: Always call the superclass implementation of onRestoreInstanceState() so the default implementation can restore the state of the view hierarchy.

这篇关于Android View-在“活动”中自动保存和恢复的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆