屏幕方向改变迫使数据重新输入
我正在开发一个简单的应用程序,只有少数活动,没有什么复杂的。我遇到的问题是,在我的一些活动中,我有 EditText 小部件,当屏幕旋转时,这些小部件会丢失它们的值。它们仍然在视觉上(即在手机屏幕上)报告旋转之前的值,但向代码报告它们的默认值。
举个例子,我有一个带有 EditText 的活动,我输入一个数字 15,计算器开始计算,显示结果。如果此时我旋转到不同的方向,正如预期的那样,活动死亡,重新加载,显示新的方向,我的值 15 仍然在 EditText 中,但是计算器(代码)计算得好像它是 0,这是初始值。我还没有尝试更改初始值来查看它是否真的从默认值中拉出,或者只是在将其转换为 int 时将其自身设置为 0。
如果我解释得不够好,请告诉我,如果需要,我可以添加源代码。 谢谢。
I'm developing a simple app, only a handful of activities and nothing to complicated. The issue I'm having is that in a few of my activities I have EditText widgets, and when the screen is rotated, these widgets loose their values. They still visually, that is, on the handset screen, report their values from before the rotation, but to the code report their default values.
An example, I have an activity with an EditText, I enter a number, 15, the calculator starts calculating, displays a result. If at this point I rotate to a different orientation, as expected the activity dies, reloads, shows the new orientation, my value of 15 is still in the EditText, however the calculator (the code) calculates as if it was 0, which is the initial value. I have not tried changing the initial value to see if it is really pulling from the default value, or just setting itself to 0 when I cast it as an int.
If I didn't explain this well enough, let me know, I can add source code if I need to.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您什么时候读取这些值?视图状态由
Activity
的onRestoreInstanceState
默认实现保留,该实现发生在onStart
和onPostCreate
之间。如果您在发生这种情况之前读取这些值,那就可以解释为什么您看到的是计算的默认值,而不是绘制屏幕后显示的值。When are you reading the values? View state is preserved by
Activity
's default implementation ofonRestoreInstanceState
, which happens betweenonStart
andonPostCreate
. If you are reading the values before this happens, that would explain why you are seeing default values for your calculation rather than the values that are displayed once the screen is drawn.