views:

130

answers:

3

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.

+1  A: 

I found the answer. To do this, in an Activity you can call setRequestedOrientation(int) with one of the values specified here: http://developer.android.com/reference/android/R.attr.html#screenOrientation

Before I kicked off my thread I called setRequestedOrientation(OFF) (OFF = nosensor) and when the thread was done I called setRequestedOrientation(ON) (ON = sensor). Works like a charm.

Scienceprodigy
A: 

As explained by Scienceprodigy in his self-answer, calling

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

and then

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

really works like charm... on real devices !

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.

Kevin Gaudin
I couldn't locate those constants. Thanks for that.
Scienceprodigy
There's an issue with these methods... It looks like if you call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); when the device is not in its default orientation usage, then the activity orientation is immediately changed (destroyed and recreated) to the device default orientation. For example, on a phone, if you hold it in landscape orientation, then the activity is switched to portrait and back to landscape when reactivating sensors. The same opposite issue with an Archos A5 IT : using it in portrait causes the activity to be switched to landscape and back to portrait.
Kevin Gaudin
The real answer to the original question is there: http://stackoverflow.com/questions/3821423/background-task-progress-dialog-orientation-change-is-there-any-100-working/3821998#3821998
Kevin Gaudin