安卓:在活动暂时禁用方向更改

安卓:在活动暂时禁用方向更改

问题描述:

我的主要活动有一定的code,使不应该被打断一些数据库的变化。我做繁重的另一个线程,并使用我设置为不可撤销的进度对话框。然而,我发现,如果我转我的手机将重新启动这是非常糟糕的正在运行的进程的活动,我也得到一个强制关闭。

My main activity has some code that makes some database changes that should not be interrupted. I'm doing the heavy lifting in another thread, and using a progress dialog which I set as non-cancellable. However, I noticed that if I rotate my phone it restarts the activity which is REALLY bad for the process that was running, and I get a Force Close.

我想要做的就是编程,直到我的过程完成,此时方向更改启用禁用屏幕方向的变化。

What I want to do is programatically disable screen orientation changes until my process completes, at which time orientation changes are enabled.

由于在他的self-answer,呼叫

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

然后

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

真正的作品般的魅力......在真实设备上!

really works like charm... on real devices !

不要认为这是打破在模拟器上测试时,按Ctrl + F11快捷键随时更改屏幕方向,没有模拟传感器动作。

Don't think that it's broken when testing on the emulator, the ctrl+F11 shortcut ALWAYS change the screen orientation, without emulating sensors moves.

编辑:这是不是最好的答案。作为评价所解释的,存在与这种方法的问题。 The真正的答案是这里。

this was not the best possible answer. As explained in the comments, there are issues with this method. The real answer is here.