Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable and enable orientation changes in an activity in Android programatically

I have an App that make some background staff. When the background work is running a progress Circle is shown, if the device is rotated during this time then the Activity is "reset" and I want to avoid that.

For this reason I decided disable orientation during this process. I have seen differends threads for this question but none with a valid solution, at least in my case.

The solutions posted are about fixing the activity orientation but you have to deal with the fact that the REVERSE orientations are not returned if you use:

getResources().getConfiguration().orientation

The function above returns SCREEN_ORIENTATION_PORTRAIT both for PORTRAIT and REVERSE_PORTRAIT cases (at least in my tests).

So at the end I used the Rotation value to deal with that, so my code "disable the rotation" is:

int rotation = getWindowManager().getDefaultDisplay().getRotation();

    switch(rotation) {
    case Surface.ROTATION_180:
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
        break;
    case Surface.ROTATION_270:
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);         
        break;
    case  Surface.ROTATION_0:
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        break;
    case Surface.ROTATION_90:
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        break;
    }

And to allow again the orientation:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

That works perfectly in a device with Android 4.1.2 but in a Device with Android 4.2.1 it is not working as expected.

I guess managing rotation in the activity live cycle should be a common issue but I have not been able to find a suitable solution. May be I'm looking to the wrong direction so any help is really welcomed.

Thanks in advance, Ivan.

like image 383
Ivan BASART Avatar asked Nov 16 '13 17:11

Ivan BASART


People also ask

How do I manage changes to screen orientation?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

How do you change orientation when retaining data?

Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.


2 Answers

If you want to disable orientation changes of the phone then you may use this code in the manifest

<activity android:name=".class_name"

//if you want your screen in portrait

android:screenOrientation="portrait" >

//if you want you screen in landscape mode

android:screenOrientation="landscape" >

and if you want your phone to change orientation but prevent the process from restarting then you can use the onConfigurationChanged method in your class:

@Override
public void onConfigurationChanged(Configuration newConfig) {
   // ignore orientation change
   if (newConfig.orientation != Configuration.ORIENTATION_LANDSCAPE) {
       super.onConfigurationChanged(newConfig);
   }
}
like image 148
ashfak Avatar answered Sep 21 '22 20:09

ashfak


Try this..

There is no wrong in your code. It's correct i checked it. Use that code in onConfigurationChanged that may give the difference and another thing is use below code to disable the activity reset.

<activity
    android:name="YourActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="adjustPan"/>  
like image 28
Hariharan Avatar answered Sep 20 '22 20:09

Hariharan