Android 锁定方向和双重创建

发布于 2024-11-30 01:57:39 字数 542 浏览 3 评论 0原文

我想让我的应用程序仅以纵向显示,因此我将 android:screenOrientation="portrait" 放入 AndroidManifest.xmlActivity 标记中code>,并将 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 放入 Activity 的 onCreate 方法中。

这可以将方向锁定为纵向,但是,当活动启动时,它会显示一次,然后再次显示,因此您会看到一种闪烁。我可以确认 onCreate 也被调用了两次。

此闪光导致稍后在我的应用程序中进一步强制关闭。

我怎样才能消除这个闪光?

编辑

我在我描述的活动之前显示了一个启动屏幕。它运行了两次,因此运行此活动两次(通过意图)两次。我通过检查此活动是否已经使用启动屏幕类中的意图来修复此问题,并且它已经不再运行它。该修复更多的是一种解决方法,而不是修复,但我希望它可以帮助人们。

I want to make my application displayed only in portrait orientation, so I have put android:screenOrientation="portrait" in the Activity tag in AndroidManifest.xml, and have put setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); in the Activity's onCreate method.

This works to lock the orientation to portrait, however, when the Activity starts, it shows itself once, then shows itself again, so you see a sort of flash. I can confirm that onCreate is being called twice as well.

This flash is causing further force closes later in my application.

How can I eliminate this flash?

Edit

I had a splash screen displaying before the activity I had described. It was being run twice, and therefore ran this activity twice (via an Intent) twice. I fixed this issue by checking if this activity had already been stared using an intent in the splash screen class, and it had, not to run it again. The fix was more of a workaround than a fix, but I hope it helps people.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

冷清清 2024-12-07 01:57:39

我相信使用 Activity 的 configChanges 属性(在清单中)应该可以解决您的问题。 ConfigChanges 属性文档

但是 ,你只是在回避另一个问题。你真正应该解决的是为什么这会引起FC?绝对没有理由发生这种情况,所以你也应该解决这个问题。更多信息请参阅处理运行时更改

请注意:使用第一种方法可以解决性能/用户体验问题。

以防万一您想知道为什么 onCreate 被调用两次,一旦我遇到这个问题,我认为这与固定方向然后创建另一个活动但方向不同有关。在第二项活动开始之前,前一项活动转变为后一项活动的方向。请记住,键盘锁也是一个活动!我不确定这是否是因为这个原因发生的。

I believe that using the Activity's configChanges attribute (in the manifest) should solve your problem. ConfigChanges attribute documentation

However, you are just going around another problem. What you really should address is why is that provoking FC? There is absolutely no reason for that to happen so you also should solve that problem. More info in handling runtime changes.

Please note: using the first approach is acceptable to address the performance/UX issues though.

Just in case you wonder why onCreate is called twice, once I got into this problem and I think that it was related to having the orientation fixed and then having another activity being created but in a different orientation. Before the second activity was started, the former activity changed into the orientation of the latter. And remember that keyguard is also an Activity! I'm not sure if this happens for this reason though.

蝶舞 2024-12-07 01:57:39

不要输入:“setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);”。
让 AndroidManifest 完成他的工作即可。我认为你所做的是:

  1. 告诉你的应用程序仅在纵向模式下运行
  2. 告诉你的活动处于纵向模式,无论它是否已经这样(闪光灯必须来自于此)。
  3. 我可能是错的。

don't put : "setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);".
Just let the AndroidManifest do his job. I thing what you did is :

  1. Tell your App to go only in Portrait mode
  2. Tell your Activity to be in Portrait mode, wherever it was already like this or not (The flash have to come from this).
  3. I could be wrong.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文