10

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 AsyncTask<String, Void, String> {

   protected String doInBackground(String... params) {
      ...
   }

   protected void onPostExecute(String result) {
      ...
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
   }

   protected void onPreExecute() {
      ...
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
   }
}

When I run these two lines of SENSOR and NOSENSOR my screen turns horizontal automatically, without understanding why. That may be happening?

EDIT: I put the following lines to check the current orientation with the following result:

   protected void onPreExecute() {
        if (getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE) {
            Log.e("TAG","LANDSCAPE");
        }else{
            Log.e("TAG","PORTRAIT");
        }
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
   }

Result of Logcat:

LANDSCAPE

But if I remove the two lines (SetRequestedOrientation) do I get this in logcat:

PORTRAIT
user370305
  • 106,566
  • 23
  • 160
  • 150
jlopez
  • 6,287
  • 2
  • 51
  • 90
  • Check this http://stackoverflow.com/questions/3821423/background-task-progress-dialog-orientation-change-is-there-any-100-working/3821998#3821998 – Vrashabh Irde Feb 05 '13 at 10:12

3 Answers3

6

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.. :-)

user370305
  • 106,566
  • 23
  • 160
  • 150
  • thanks for answering. The same problem, change to landscape :S – jlopez Feb 05 '13 at 10:11
  • Actually, Its for the same configuration orientation (Locked) for your work time. – user370305 Feb 05 '13 at 10:12
  • Interesting to know but remember what you wrote here the next time you go to rotate your mahussive phone for a nicer grip only to watch it ignore you as it sits fixed in the previous orientation...the anguish, the horror :) – BrantApps Feb 05 '13 at 12:20
0

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.

Chris.Jenkins
  • 12,913
  • 4
  • 59
  • 60
  • the property screenSize is for Android 4.0.3. and i'm using Android 2.3.3 – jlopez Feb 05 '13 at 10:18
  • 1
    It does not matter, It is always advised to Build/Target the Latest API (17 at time of writing) then set your minimum (to something like API8), this makes sure you app does not run in compatibility mode on newer devices. And means you include flags as the one above the the other 50% of your users. – Chris.Jenkins Feb 05 '13 at 10:36
0

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.

Community
  • 1
  • 1
BrantApps
  • 6,274
  • 2
  • 24
  • 60
  • Does his explanation seems very reasonable, but as I can put it into practice? I do not understand very well how to do this – jlopez Feb 05 '13 at 10:35
  • Have a read of the portion of the developer guide on [_bound_ services](http://developer.android.com/guide/components/bound-services.html) and then look into making your service an [Intent service](http://developer.android.com/reference/android/app/IntentService.html) - this special type of service kills itself after the underlying async task has completed. This removes a fair amount of boiler plate code- wish I had discovered it earlier. – BrantApps Feb 05 '13 at 10:41
  • I wish I could implement your solution, but it seems complex and I don't have much time. – jlopez Feb 05 '13 at 10:46
  • It's a bit more code I must admit but this is a tricky part of the framework. If you have to get something out the door and fast then you'll need to use one of the Manifest Activity orientation hacks. Something like already suggested: android:configChanges="orientation|keyboard" – BrantApps Feb 05 '13 at 10:52