Android Best Practices: How to let an activity start in any orientation but not allow orientation change
I am writing an Android application that will run on phones and H开发者_高级运维oneycomb tablets, and pretty much runs different activities based on the OS and right from the get-go. On non-honeycomb devices, I have all of my activities locked to portrait for ease of UI, and the same thing goes for honeycomb, only they're all locked to landscape, again for better UI and user experience.
I have some dialog-themed activities, such as a file browser that I wrote, that I'd like to use on either device, and have the dialog activity be the same orientation as the current application. So if the user is using a phone, the dialog would show up in portrait, they could select their file, and the activity would disappear and the user would continue. If they are using a Honeycomb device, the dialog activity would appear in landscape and the same rules would apply.
I don't want to allow the orientation to change dynamically, however. If the file browser activity is on screen, the user should not be allowed to rotate the device, because the underlying activities don't support it.
I'm looking for suggestions on how to accomplish this.
You can add android:configChanges="orientation"
on the activity in the AndroidManifest to inform the OS that you are handling orientation changes. Usually we would implement some kind of handling in the onConfigurationChanged
method, but in this case you are doing nothing.
Just a thought, may or may not work:
WindowManager wm = (WindowManager) activity
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
display.getRotation();
Surface.setOrientation(display.getDisplayId(), display.getRotation());
See if this works :)
精彩评论