Android:在屏幕方向更改时保存应用程序状态
在发布这个问题之前我已经看到了以下链接
http://www.devx.com/无线/文章/40792/1954
使用保存实例状态保存 Android Activity 状态
http://www.gitshah.com/2011/03/how-to-handle-screen-orientation_28.html
如果状态由我的设备构成,如何在 Android 中的方向更改期间保存状态类?
我不知道应该如何重写以下函数:
@Override
public Object onRetainNonConfigurationInstance() {
return someExpensiveObject;
}
在我的应用程序中,我的布局中一个 editext 可见,当第一个 editext 的数据验证为 true 时,其他 editext 可见。我已设置所有的可见性其他 editextes 和 textview 为 false 并使其在验证后可见。
因此,在我的活动中,如果屏幕方向发生更改,则所有具有 android:visibility="false"
的项目都会变得不可见。
我还知道,当我们的活动屏幕方向发生变化时,它会调用 onStop() ,然后调用 onDestroy() ,然后通过调用 onCreate() 再次启动一个新的活动,
这就是原因..但我不知道如何解决它..
在这里您可以看到我的应用程序的屏幕截图:
在此图像中,所有字段均已加载 在另一张图像中,当屏幕方向更改为横向时,它们都消失了
教程或片段的任何链接代码将非常有价值。
而且当显示进度对话框并且我尝试更改屏幕方向时,我的应用程序崩溃了。如何处理这个问题?
谢谢
I have seen the following links before posting this question
http://www.devx.com/wireless/Article/40792/1954
Saving Android Activity state using Save Instance State
http://www.gitshah.com/2011/03/how-to-handle-screen-orientation_28.html
How to save state during orientation change in Android if the state is made of my classes?
I am not getting how should i override the following function :
@Override
public Object onRetainNonConfigurationInstance() {
return someExpensiveObject;
}
In my application i have layout with one editext visible and other editext get visible when the data of first editext validates to true.I have set the visbility of all other editextes and textviews to false and make them visible after validating.
So in my activity if the screen orientation is changed then all the items having android:visibility="false"
get invisible.
I have also came to know that when our activities screen orientation changes it calls onStop() followed by onDestroy() and then again starts a fresh activity by calling onCreate()
This is the cause .. But i am not getting how to resolve it ..
Here You can see the screenshots of my application :
in this image all fields are loaded
and in another image when the screen orientation is changed to landscape they are all gone
Any link to tutorial or piece of code will be highly appreciable.
And also my application crashes when a progress dialog is shown up and i try to change screen orientation.How to handle this ??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果两个屏幕的布局相同,则无需这样做,只需在
Activity
节点的manifest
中添加以下行: Android 3.2(API 级别 13)及更高版本:
从此处的文档: http://developer.android.com/guide/主题/清单/activity-element.html
Well if you have the same
layout
for both screens then there is no need to do so just add below line in yourmanifest
inActivity
nodefor Android 3.2 (API level 13) and newer:
From documentation here: http://developer.android.com/guide/topics/manifest/activity-element.html
还有另一种可能性,您可以使用 onConfigurationChanged(Configuration newConfig) 来保持状态,甚至在方向更改时也是如此。
当您的 Activity 运行时设备配置发生更改时,由系统调用。请注意,只有当您选择了要使用清单中的 configChanges 属性处理的配置时,才会调用此方法。如果发生任何未选择由该属性报告的配置更改,则系统将停止并重新启动活动(以使用新配置启动它),而不是报告它。
调用此函数时,您的资源对象将被更新以返回与新配置匹配的资源值。
There is another possibility using which you can keep the state as it is even on Orientation change using the onConfigurationChanged(Configuration newConfig).
Called by the system when the device configuration changes while your activity is running. Note that this will only be called if you have selected configurations you would like to handle with the configChanges attribute in your manifest. If any configuration change occurs that is not selected to be reported by that attribute, then instead of reporting it the system will stop and restart the activity (to have it launched with the new configuration).
At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.
有两种方法可以做到这一点,第一种是在
AndroidManifest.xml
文件中。您可以将其添加到您的活动标签中。 本文档将为您提供深入的解释,但简单地说使用这些值并告诉活动在这些值之一发生更改时不要重新启动。第二个是:覆盖
onSaveInstanceState
和onRestoreInstanceState
。这种方法需要更多的努力,但可以说更好。onSaveInstanceState
会在 Activity 终止之前保存 Activity 设置的值(由开发人员手动设置),而onRestoreInstanceState
在onStart()
之后恢复该信息 请参阅官方文档以进行更深入的了解。您不必实现onRestoreInstanceState
,但这需要将该代码粘贴到onCreate()
中。在下面的示例代码中,我保存了 2 个 int 值、微调器的当前位置以及单选按钮。
There are 2 ways of doing this, the first one is in the
AndroidManifest.xml
file. You can add this to your activity's tag. This documentation will give you an in depth explanation, but put simply it uses these values and tells the activity not to restart when one of these values changes.And the second one is: overriding
onSaveInstanceState
andonRestoreInstanceState
. This method requires some more effort, but arguably is better.onSaveInstanceState
saves the values set (manually by the developer) from the activity before it's killed, andonRestoreInstanceState
restores that information afteronStart()
Refer to the official documentation for a more in depth look. You don't have to implementonRestoreInstanceState
, but that would involve sticking that code inonCreate()
.In my sample code below, I am saving 2
int
values, the current position of the spinner as well as a radio button.