onSaveInstanceState 不保存我的值( onCreate input Bundle 始终为 null )
保存包(活动 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我通过以下方式解决了这个问题:
重新启动具有后台保存状态的旧意图时。如果没有这个标志,我认为当调用 startActivity 时,它会创建 Activity 的一个新实例,而不是从堆栈中获取旧实例。
I got this solve by having:
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.
你的代码没有任何问题。尝试在活动 A 时旋转设备。您将在日志中看到以下内容,这意味着您的 onSaveInstanceState() 工作正常:
以下是 Android 开发者网站,您可能会感兴趣:
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:
Here are an excerpt from the Android Developer Site, which you may find interesting:
在向其中添加内容之前,您必须执行 super.onSaveInstanceState(outState) 。
You have to do super.onSaveInstanceState(outState) before adding content to it.
如果导航到另一个 Activity 并返回,最好的方法是将其保存到
onPause()
方法中的SharedPreference
中,该方法必然在加载新 Activity 时执行。在另一个Activity
上,可以通过访问共享首选项在onCreate()
中访问该值。If navigating to another Activity and coming back, best way is to save it to
SharedPreference
in theonPause()
method, which is bound to execute when a new activity is loaded. On the otherActivity
, the value can be accessed inonCreate()
by accessing the shared preference.如果您想在设备旋转时保留片段的状态,并且您想知道永远不会调用
onSaveInstanceState
:super.onSaveInstanceState(outState);
在所有onSaveInstanceState
函数中。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:super.onSaveInstanceState(outState);
in all youronSaveInstanceState
functions.android:configChanges
in your AndroidManifest.xml.除非您的应用程序在 Lollipop (API21) 版本的 Android 或更高版本上运行,否则您的应用程序
不会被调用,因为它根本不存在于 21 之前的平台版本上。要支持 API 21 之前的设备,您必须代替上述设备,重写以下方法:
这也适用于 API 21+,因此您当然不需要重写这两种方法(除非您知道需要处理新方法提供的 PersistableBundle)。
(复制是因为它对我有用)
来源:屏幕旋转后不会调用onSaveInstanceState
Unless your app is running on Lollipop (API21) version of Android or newer, your
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:
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