How to handle rotation?
Now if you choose to rotate your activity with display either specifying “rotate as sensor” or using “orientation setting” you must handle the steps that are taken when the display is rotated.
Whenever the window rotates, your activity is destroyed and recreated, and “onCreate(Bundle)” is executed again.
You may need to save the activity state before this happens. To achieve that you can override the
public void onSaveInstanceState(Bundle outState)
For Example: You may want to save the Value of a String array before screen orientation change restarts your activity.
To do this override the onSaveInstanceState method of Activity class like this:
public class SampleActivity extends Activity{
String[] testArray = null;
public static final String TEST_ARRAY = “KEY_FOR_ARRAY”;
@Override
public void onCreate(Bundle State){
if(State != null){
testArray =
State.getStringArray(SampleActivity.TEST_ARRAY);
}
else{
testArray = new String[6];
}
}
@Override
public void onSaveInstanceState(Bundle outstate){
outstate.putStringArray(SampleActivity.TEST_ARRAY,
testArray);
}
}
Otherwise your activity will use the default implementation of this function which saves some amount of state.
Some of the values that you can save before orientation change are as follows:
- Serializable and its subclasses.
- String
- Parcelable and its Subclasses
- boolean, int, float.
- Arrays etc.
However you may not want to store the values because this may slow down your application you can attain faster screen orientation by caching the data before orientation changes.
Your activity can cache data before rotation this can be accomplished by overriding
public Object onRetainNonConfigurationInstance()
Let’s take the above example again but instead of saving the String array we’ll cache the data and retrieve reference to that after restart.
public class SampleActivity extends Activity{
String[] testArray = null;
public static final String TEST_ARRAY = “KEY_FOR_ARRAY”;
@Override
public void onCreate(Bundle State){
testArray = (String[])getLastNonConfigurarionInstance();
if(testArray == null)
testArray = new String[6];
}
@Override
public Object onRetainNonConfigurationInstance(){
return testArray;
}
}
Change orientation at runtime
You can use Activity.setRequestedOrientation() from your activity to set that orientation
e.g. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if you do not use this the default value is SCREEN_ORIENTATION_UNSPECIFIED and you must not specify any orientation in Applications manifest XML. And each time orientation is changed the activity is stopped and restarted.
Check orientation settings
You can query the orientation settings for device using
public static int myOrient;
myOrient = this.getResources().getConfiguration().orientation;
this function will return either of the following values:
ORIENTATION_LANDSCAPE or ORIENTATION_PORTRAIT, it can also return ORIENTATION_SQUARE on a square device.
<<Handling Screen Orientation - Part 1
Cheers!!
Vivek Jain
Hello Everyone, I came across an application which allowed me to produce nice effects on Images that are in my phone and Images that I take from my phones camera, its available here,