Lock orientation until Asynctask finish

a 夏天 提交于 2020-01-01 02:34: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

回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/14704774/lock-orientation-until-asynctask-finish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!