Android 中处理方向变化的正确方法
Android 中处理方向变化的正确方法是什么?当我研究这个问题时,出现了两种方法。
第一种方法 使用方法 onSaveInstanceState(Bundle savingInstanceState)
和 onRestoreInstanceState(Bundle savingInstanceState)
来存储和恢复在方向更改后被 Android 操作系统终止的 Activity。
第二种方法 将 android:configChanges="orientation|keyboardHidden"
添加到您的 AndroidManifest.xml,这样当方向更改时 Activity 不会被销毁。
我已经尝试了这两种方法并且它们都有效,但是第一种方法需要更长的时间来实现。虽然我确实看到了有关第二种方法的帖子,但我想知道这是否是处理方向变化的“可接受的”和“正确的”方法。每种方法的优点和缺点是什么?谢谢!
What is the proper way of handling an orientation change in Android? When I researched this question there are two methods that came up.
1st Method
Use the methods onSaveInstanceState(Bundle savedInstanceState)
and onRestoreInstanceState(Bundle savedInstanceState)
to store and restore your Activity after being killed by the Android OS after the orientation change.
2nd Method
Added android:configChanges="orientation|keyboardHidden"
to your AndroidManifest.xml so the Activity will not be destroyed when the orientation is changed.
I have tried both methods and they both work, however the first method takes a lot longer to implement. While I do see posts about the 2nd method, I want to know if this is an "accepted" and "proper" way of handling an orientation change. And what are the advantages and disadvantages for each method? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第二种方法不允许您执行某些特定于方向的操作,例如在屏幕旋转或不旋转时加载不同的布局(我在这里考虑资源后缀)。我没有遇到任何其他不良影响,但是文档指出:“应避免使用此属性,并且仅将其用作最后的手段。”
更多信息在这里: http://developer.android.com/guide/topics /resources/runtime-changes.html
The second method will not allow you to do certain orientation specific stuff such as load a different layout for when the screen is rotated or not (I'm thinking of resource suffixes here). I have not encountered any other ill effects, however the docs state that: "Using this attribute should be avoided and used only as a last-resort."
More info here: http://developer.android.com/guide/topics/resources/runtime-changes.html
请参阅http://developer.android.com/guide/topics/resources/ runtime-changes.html 他们解释了这两种方法并给出了优缺点和最佳解决方案。
See http://developer.android.com/guide/topics/resources/runtime-changes.html where they explain both methods and give the pros and cons and best solution.