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:
<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 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 );
}
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.
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.
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.
<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.
Forcing Orientation at Run Time:
int current = getRequestedOrientation();
// only switch the orientation if not in portrait
if ( current != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ) {
setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
}
No comments:
Post a Comment