Current Android Release version:
Version Code: Pie
Version: 9
API level: 28

Showing posts with label configuration change. Show all posts
Showing posts with label configuration change. Show all posts

Activity Auto rotation in Android

By Default,when you rotate the device android system will destroy the current activity and instantiating the new instance of same activity.

By default, the new activity won’t have the runtime state of the old. Your activity must override onSaveInstanceState() to save runtime state from the old instance into a Bundle object. Android passes this bundle into the onCreate() method of the new activity instance, where you can use it to re-populate your member variables and onscreen controls.

Handling Orientation Changes:


         if you don't like android default method for handling screen rotation,you can set the android:configChanges flag in AndroidManifest.xml

<activity android:name="com.example.MyActivity"
             android:label="@string/app_name"
             android:configChanges="orientation" >
     <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
</activity>


Orientation flag in android:configChanges tells the system that the Activity wishes to handle rotation on its own. Android will invoke the onConfigurationChanged() method of your Activity instead, which allow you to handle the screen rotation yourself.


Forcing Orientation at Build Time:
          
         If you wish to force an Activity to always be a particular orientation, you can simply specify its orientation in its entry in AndroidManifest.xml:

<activity android:name="com.example.MyActivity"
             android:label="@string/app_name"
             android:screenOrientation="landscape"
             android:configChanges="orientation" >
     <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
</activity>


The android:screenOrientation attribute forces this activity to be in landscape mode always.

If you use android:screenOrientation to hammer the orientation to portrait or landscape, I highly recommend you combine it with the android:configChanges="orientation" flag to ensure that Android will not kill/restart your Activity on rotation.

When you combine the two flags, you can likely leave your implementation of onConfigurationChanged() blank, as your Activity has nothing to do on screen rotations.

Forcing Orientation at Run Time:

You might have a need to dynamically lock the orientation – that is, choose between landscape or portrait at runtime, rather than build time. You can use the method Activity#setRequestedOrientation() for this purpose.

int current = getRequestedOrientation();
// only switch the orientation if not in portrait
if ( current != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ) {
       setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
}

Android Configuration changes

When you rotate the device your Activity will re-create and all the variables will be re-initialized. So, in that case if you want to some values to remain same on Rotation also you can store their state using the onSaveInstanceState() and you can restore in onCreate() again by checking Bundle is not null.

Normally upon configuration change, the current activity is destroyed automatically (the methods onPause(), onStop(), and onDestroy() are called) and a new activity is created (the methods onCreate(), onStart(), and onResume() are called) for the new configuration.


Any change to the GUI for the new configuration, if necessary, should be done manually.

if(savedInstanceState != null)

 // get the restore value from the Bundle 

}

Where as onConfigurationChanged() will be called when you rotate the Device(Note that this will only be called if you have selected configurations you would like to handle with the configChanges attribute in your manifest). From the parameter you will get the new device configuration.

If you don't want your Activity to get re-created on rotation of Device then you have to add this line to your activity tag in the AndroidManifest file.   

android:ConfigChanges="keyboardHidden|orientation"