简体   繁体   English

Android:在屏幕方向更改时保存应用程序状态

[英]Android : Save application state on screen orientation change

I have seen the following links before posting this question在发布此问题之前,我已经看到以下链接

http://www.devx.com/wireless/Article/40792/1954 http://www.devx.com/wireless/Article/40792/1954

Saving Android Activity state using Save Instance State 使用 Save Instance State 保存 Android Activity 状态

http://www.gitshah.com/2011/03/how-to-handle-screen-orientation_28.html 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? 如果状态由我的类组成,如何在 Android 中的方向更改期间保存状态?

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.在我的应用程序中,当第一个 editext 的数据验证为 true 时,我的布局有一个 editext 可见,其他 editext 可见。我已将所有其他 editextes 和 textviews 的可见性设置为 false 并在验证后使它们可见。

So in my activity if the screen orientation is changed then all the items having android:visibility="false" get invisible.因此,在我的活动中,如果屏幕方向发生变化,则所有具有android:visibility="false"的项目都将不可见。

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()我也开始知道,当我们的活动屏幕方向改变时,它调用 onStop() 然后调用 onDestroy() 然后通过调用 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谢谢

Well if you have the same layout for both screens then there is no need to do so just add below line in your manifest in Activity node好吧,如果您对两个屏幕都有相同的layout ,则无需这样做,只需在Activity节点的manifest中添加以下行

android:configChanges="keyboardHidden|orientation"

for Android 3.2 (API level 13) and newer:对于 Android 3.2(API 级别 13)及更高版本:

android:configChanges="keyboardHidden|orientation|screenSize"

because the "screen size" also changes when the device switches between portrait and landscape orientation.因为当设备在纵向和横向之间切换时,“屏幕尺寸”也会发生变化。 Documentation here: http://developer.android.com/guide/topics/manifest/activity-element.html这里的文档: http : //developer.android.com/guide/topics/manifest/activity-element.html

There is another possibility using which you can keep the state as it is even on Orientation change using the onConfigurationChanged(Configuration newConfig).还有另一种可能性,您可以使用 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.请注意,只有在您选择了要使用清单中的 configChanges 属性处理的配置时,才会调用此方法。 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.在调用此函数时,您的 Resources 对象将被更新以返回与新配置匹配的资源值。

There are 2 ways of doing this, the first one is in the AndroidManifest.xml file.有两种方法可以做到这一点,第一种是在AndroidManifest.xml文件中。 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. 本文档将为您提供深入的解释,但简单地说,它使用这些值并告诉活动在这些值之一发生更改时不要重新启动。

android:configChanges="keyboardHidden|orientation|screenSize|screenLayout"

And the second one is: overriding onSaveInstanceState and onRestoreInstanceState .第二个是:覆盖onSaveInstanceStateonRestoreInstanceState 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, and onRestoreInstanceState restores that information after onStart() Refer to the official documentation for a more in depth look. onSaveInstanceState在 Activity 被杀死之前保存(由开发人员手动)设置的值,而onRestoreInstanceStateonStart()之后恢复该信息。 请参阅官方文档以获得更深入的了解。 You don't have to implement onRestoreInstanceState , but that would involve sticking that code in onCreate() .您不必实现onRestoreInstanceState ,但这将涉及将该代码粘贴在onCreate()

In my sample code below, I am saving 2 int values, the current position of the spinner as well as a radio button.在下面的示例代码中,我保存了 2 个int值、微调器的当前位置以及一个单选按钮。

 @Override
    public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
        spinPosition = options.getSelectedItemPosition();
        savedInstanceState.putInt(Constants.KEY, spinPosition);
        savedInstanceState.putInt(Constants.KEY_RADIO, radioPosition);
        super.onSaveInstanceState(savedInstanceState);

    }

    // And we restore those values with `getInt`, then we can pass those stored values into the spinner and radio button group, for example, to select the same values that we saved earlier. 

    @Override
    public void onRestoreInstanceState(@NotNull Bundle savedInstanceState) {
        spinPosition = savedInstanceState.getInt(Constants.KEY);
        radioPosition = savedInstanceState.getInt(Constants.KEY_RADIO);
        options.setSelection(spinPosition, true);
        type.check(radioPosition);
        super.onRestoreInstanceState(savedInstanceState);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM