Android Lock Orientation and Double Create

China☆狼群 提交于 2019-12-13 02:05:07

问题


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.


回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/7084695/android-lock-orientation-and-double-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!