开发者

Android Lock Orientation and Double Create

开发者 https://www.devze.com 2023-03-28 18:46 出处:网络
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 setRequestedOrientat

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 开发者_JAVA百科than a fix, but I hope it helps people.


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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号