Only want to block orientation until all the data is loaded in my view. So I thought about the following code but it doesn\'t work.
private class task extends As
Probably a bug, but maybe a better way would be to either lock your orientation so you dont need to worry about the activity being created... OR better yet.
Use this in your manifest for the activity:
android:configChanges="orientation|screenSize|screenLayout"
Then your activity isn't recreated on rotate (also as of HoneyComb/3.0 Activities aren't re-created on rotate).
Just override the:
onConfigurationChanged(Configuration newConfig){
//Handle new orientation
}
Then you don't need to confuse the user why there phone has stopped rotating during an operation.
Also worth looking at Robospice if you want lifecyled async tasks.
You are doing this because you have found that your async task is killed during the activity destroy and recreate. Am I right?
The correct way of handling this is not to lock the orientation whilst your task is running. I don't agree that the setRequestedOrientation
approach not working is a bug; rather the idea of locking an activities orientation and then unlocking from an async task is unsupported.
I have gone to explain in some detail how to ensure your async task is not killed by disassociating it with your activity lifecycle. Looking back on the solution I provided I have since discovered better ways of checking a service is running (Inter Process Control and bound services) but the overarching principle applies. Host the async task in an orientation unaware android.app.Service
and allow the async task to only communicate with the service and the service to only communicate with those activities which have bound to it.
I think this is a more standard approach.
Just replace setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
with setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
And let me know what happen..
For Entry Level
protected void onPreExecute() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
}
At Exit Level
protected void onPostExecute(String result) {
...
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
Update:
Wired behavior (In your case) but, any way you can get the current orientation using,
int currentOrientation = getResources().getConfiguration().orientation;
Now set this orientation in onPreExecute()
..
Like,
protected void onPreExecute() {
...
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
}
Simple.. :-)