Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock orientation until Asynctask finish

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
like image 883
jlopez Avatar asked Feb 05 '13 10:02

jlopez


People also ask

How can we overcome the AsyncTask limitations?

There are two simple solutions that cover most situations: Either check AsyncTask. isCancelled() on a regular basis during your long-running operation, or keep your Thread interruptible. Either way, when you call AsyncTask. cancel() these methods should prevent your operation from running longer than necessary.

What ll happen when rotate the screen while async task running in background?

Solution 1 Rotation of the screen will cause the current Activity to be destroyed and new one recreated. If an AsyncTask is running when this happens, it could give rise to two consequences: 1.

How do I stop my Android phone from rotating?

Short guide: Tap the Settings icon to open the Settings app. Scroll down and tap Accessibility. Scroll down to Interaction controls and tap Auto-rotate screen to turn it off.


1 Answers

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

like image 69
user370305 Avatar answered Nov 09 '22 23:11

user370305