强制 Android 在方向改变时不重绘活动 [英] Forcing Android to not redraw activity on orientation change

查看:38
本文介绍了强制 Android 在方向改变时不重绘活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决这个问题.
虽然我已经阅读了很多关于 Orientation Change 的内容,但 Android 会杀死一个活动并将其作为一个新活动启动,而处理这个问题的唯一方法是将所有内容保存在 onSaveInstanceState() 中并尝试在 onCreate() 中恢复它.

I have been going gaga to figure this out.
Although I have read a lot that on Orientation Change, Android kills an activity and starts it as a fresh one, and the only way to handle this is to save all the stuff inside onSaveInstanceState() and try to restore it inside onCreate().

但我的活动在不同时间进行了很多不同类型的网络活动,如果在执行网络活动时改变方向,我将不得不处理许多不同且复杂的场景.

But my activity does a lot and different kind of network activities at different times and if the orientation is changed when the network activity is being performed, I'll have to handle a lot of different and complex scenarios.

有没有什么简单的方法可以让 Android 指出当方向改变时这个 Activity 根本不需要重新绘制,以便它自动保存所有数据并重新使用它?

Is there any simple way to just point Android that this activity doesn't need to be redrawn at all when the orientation is changed so that it automatically saves all the data and re-uses it?

我想知道有没有类似的东西.

I wonder if there's any thing like that.

推荐答案

是的,您可以在 AndroidManifest.xml<中的活动声明中添加属性 android:configChanges="orientation"/code> 文件.

Yes, you can add attribute android:configChanges="orientation" to the activity declaration in the AndroidManifest.xml file.

编辑:android:configChanges 属性的目的是防止在真正需要时重新创建活动.例如,Camera 应用程序使用此属性是因为在发生方向更改时不得重新创建相机预览屏幕.用户希望在旋转设备时相机预览不会有任何延迟,并且相机初始化不是一个非常快的过程.因此,手动处理方向更改是相机应用程序的一种本机行为.

EDIT: The purpose of the android:configChanges attribute is to prevent an activity from being recreated when it's really necessary. For example the Camera application uses this attribute because it the camera preview screen mustn't be recreated when an orientation change happens. Users expect the camera preview to work without any delays when they rotate their devices and camera initialization is not a very fast process. So it's kind of a native behavior for the Camera application to handle orientation changes manually.

对于大多数应用程序,在方向更改期间是否重新创建活动并不重要.但有时由于活动创建缓慢、活动执行的异步任务或其他一些原因,在此过程中持久化活动会更方便.在这种情况下,可以稍微调整应用程序并使用 android:configChanges="orientation" 属性.但是,当您使用此调整时,真正重要的是要了解您必须实现正确保存和恢复状态的方法!

For most applications it doesn't really matter if an activity is recreated or not during orientation changes. But sometimes it's more convenient to persist an activity during this process because of slow activity creation, asynchronous tasks performed by an activity or some other reasons. In this case it's possible to tweak an application a little and to use the android:configChanges="orientation" attribute. But what is really important to understand when you use this tweak is that you MUST implement methods for saving and restoring a state properly!

所以总结一下这个答案,android:configChanges 可以让你提高应用程序的性能或在一些罕见的情况下使其行为本机",但它不会减少您必须编写的代码量.

So to sum up this answer, the android:configChanges can allow you to improve the performance of an application or to make it behave "natively" in some rare cases but it doesn't reduce the amount of code you have to write.

这篇关于强制 Android 在方向改变时不重绘活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆