gpt4 book ai didi

java - Android Activity状态,无法获取保存的信息

转载 作者:行者123 更新时间:2023-12-01 14:51:52 25 4
gpt4 key购买 nike

关于这个主题还有很多其他问题,但我一直无法解决这个问题。

在 Android 文档 (http://developer.android.com/training/basics/activity-lifecycle/recreating.html) 中,它说:

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 object). 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.

因此,我通过简单地创建一个包含 EditText View 的 View 来在模拟器中对此进行测试。然后我在其中输入信息并按主页按钮。当我重新打开应用程序时,信息消失了。这不应该自动保留还是我错过了什么?

最佳答案

嗯,你部分错误,部分正确。你错了,因为灰色的引文是断章取义的。我将通过从您提供的链接中正确引用来简要解释:

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.

现在,在该段落之后我们有一个澄清:

Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

另外一个,下面几行是:

To save additional data about the activity state, you must override the onSaveInstanceState() callback method. The system calls this method when the user is leaving your activity and passes it the Bundle object that will be saved in the event that your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle object to both the onRestoreInstanceState() and onCreate() methods.

当应用程序意外崩溃或启用屏幕旋转(仅举几例)时,会使用此 bundle ( bundle savedInstanceState),这也会破坏(然后重新创建)您的前景。

您还可以查看以下部分“保存您的 Activity 状态”,但我会向您推荐此链接 here .

关于java - Android Activity状态,无法获取保存的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14769769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com