onSaveInstanceState 不保存我的值( onCreate input Bundle 始终为 null )

发布于 2024-12-03 10:36:34 字数 776 浏览 0 评论 0 原文

保存包(活动 A):

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("test", "value");
    super.onSaveInstanceState(outState);
}

导航至活动 B;

startActivity(new Intent(getBaseContext(), B.class));

回到活动 A:

startActivity(new Intent(getBaseContext(), A.class));

尝试加载我在活动 A 中编写的绑定值:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

Log.d("MY", "saved instance is null"+ Boolean.toString(savedInstanceState == null));
}

始终返回 savingInstanceState = null。我在这里缺少什么?

当我返回主活动时,永远不会触发 onRestoreInstanceState

Saving bundle (activity A):

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("test", "value");
    super.onSaveInstanceState(outState);
}

Navigating to activity B;

startActivity(new Intent(getBaseContext(), B.class));

Going back to activity A:

startActivity(new Intent(getBaseContext(), A.class));

Trying to load the value I wrote to bundle in activity A:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

Log.d("MY", "saved instance is null"+ Boolean.toString(savedInstanceState == null));
}

Returns always savedInstanceState = null. What I'm missing here?

onRestoreInstanceState is never triggered when I return to main activity

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我很坚强 2024-12-10 10:36:34

我通过以下方式解决了这个问题:

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

重新启动具有后台保存状态的旧意图时。如果没有这个标志,我认为当调用 startActivity 时,它会创建 Activity 的一个新实例,而不是从堆栈中获取旧实例。

I got this solve by having:

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

when re-launching the old intent that has the saved state from background. Without this flag, I think that when startActivity is called, it creates a new instance of the Activity instead of getting the old one from the stack.

凉墨 2024-12-10 10:36:34

你的代码没有任何问题。尝试在活动 A 时旋转设备。您将在日志中看到以下内容,这意味着您的 onSaveInstanceState() 工作正常:

saved instance is nullfalse

以下是 Android 开发者网站,您可能会感兴趣:

可以保存有关 Activity 当前状态信息的回调方法是 onSaveInstanceState()。系统在使活动容易被破坏之前调用此方法,并向其传递一个 Bundle 对象。 Bundle 是您可以使用 putString() 等方法将有关活动的状态信息存储为名称/值对的位置。然后,如果系统终止您的 Activity 进程并且用户导航回您的 Activity,系统会将 Bundle 传递给 onCreate(),以便您可以恢复在 onSaveInstanceState() 期间保存的 Activity 状态。如果没有要恢复的状态信息,则传递给 onCreate() 的 Bundle 为 null。

注意:不能保证 onSaveInstanceState() 在您的 Activity 被销毁之前被调用,因为在某些情况下不需要保存状态(例如当用户使用 BACK 键离开您的 Activity 时) ,因为用户明确关闭该活动)。如果调用该方法,则始终在 onStop() 之前调用,也可能在 onPause() 之前调用。

There is nothing wrong with your code. Try rotating the device when on activity A. You will see the following in the Log, which means your onSaveInstanceState() is working fine:

saved instance is nullfalse

Here are an excerpt from the Android Developer Site, which you may find interesting:

The callback method in which you can save information about the current state of your activity is onSaveInstanceState(). The system calls this method before making the activity vulnerable to being destroyed and passes it a Bundle object. The Bundle is where you can store state information about the activity as name-value pairs, using methods such as putString(). Then, if the system kills your activity's process and the user navigates back to your activity, the system passes the Bundle to onCreate() so you can restore the activity state you saved during onSaveInstanceState(). If there is no state information to restore, then the Bundle passed to onCreate() is null.

Note: There's no guarantee that onSaveInstanceState() will be called before your activity is destroyed, because there are cases in which it won't be necessary to save the state (such as when the user leaves your activity using the BACK key, because the user is explicitly closing the activity). If the method is called, it is always called before onStop() and possibly before onPause().

巷子口的你 2024-12-10 10:36:34

在向其中添加内容之前,您必须执行 super.onSaveInstanceState(outState) 。

You have to do super.onSaveInstanceState(outState) before adding content to it.

掀纱窥君容 2024-12-10 10:36:34

如果导航到另一个 Activity 并返回,最好的方法是将其保存到 onPause() 方法中的 SharedPreference 中,该方法必然在加载新 Activity 时执行。在另一个 Activity 上,可以通过访问共享首选项在 onCreate() 中访问该值。

If navigating to another Activity and coming back, best way is to save it to SharedPreference in the onPause() method, which is bound to execute when a new activity is loaded. On the other Activity, the value can be accessed in onCreate() by accessing the shared preference.

马蹄踏│碎落叶 2024-12-10 10:36:34

如果您想在设备旋转时保留片段的状态,并且您想知道永远不会调用 onSaveInstanceState

  1. 检查您是否调用 super.onSaveInstanceState(outState); 在所有 onSaveInstanceState 函数中。
  2. 检查 AndroidManifest.xml 中是否有 android:configChanges

In case you want to retain the state of your fragment over a rotation of the device and you wonder that onSaveInstanceState is never called:

  1. Check whether you call super.onSaveInstanceState(outState); in all your onSaveInstanceState functions.
  2. Check that you don't have android:configChanges in your AndroidManifest.xml.
花开半夏魅人心 2024-12-10 10:36:34

除非您的应用程序在 Lollipop (API21) 版本的 Android 或更高版本上运行,否则您的应用程序

public void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState);

不会被调用,因为它根本不存在于 21 之前的平台版本上。要支持 API 21 之前的设备,您必须代替上述设备,重写以下方法:

public void onSaveInstanceState (Bundle outState);

这也适用于 API 21+,因此您当然不需要重写这两种方法(除非您知道需要处理新方法提供的 PersistableBundle)。

(复制是因为它对我有用)
来源:屏幕旋转后不会调用onSaveInstanceState

Unless your app is running on Lollipop (API21) version of Android or newer, your

public void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState);

will NOT be called as it simply does not exist on earlier versions of the platform than 21. To support pre API 21 devices you must, instead of the above, override the following method:

public void onSaveInstanceState (Bundle outState);

This will work on API 21+ as well, so you do not need to override both methods, of course (unless you know you need to deal with PersistableBundle the new one offers).

(Copied because it worked for me)
Source: onSaveInstanceState is not getting called after screen rotation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文