Android 中处理方向变化的正确方法

发布于 2024-11-17 06:47:05 字数 515 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

庆幸我还是我 2024-11-24 06:47:05

第二种方法不允许您执行某些特定于方向的操作,例如在屏幕旋转或不旋转时加载不同的布局(我在这里考虑资源后缀)。我没有遇到任何其他不良影响,但是文档指出:“应避免使用此属性,并且仅将其用作最后的手段。”

更多信息在这里: 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

春夜浅 2024-11-24 06:47:05

请参阅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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文