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
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.
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.
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.
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.. :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With