Currently, I have a parent Activity, which its orientation can be either landscape mode or portrait mode, depending on device Accelerometer.
It is going to launch a child Activity, which its orientation is always in landscape mode.
When the user quits from child Activity, I which parent Activity can immediately restore its original orientation.
I try the following mythology. It doesn't work.
public class ParentActivity extends SherlockFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (savedInstanceState == null) {
} else {
int orientation = savedInstanceState.getInt(ORIENTATION_KEY);
// **Orientation is completely detached from Accelerometer**
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
@Override
protected void onSaveInstanceState (Bundle savedInstanceState) {
// Calling super.onSaveInstanceState is important.
super.onSaveInstanceState(savedInstanceState);
// **Too late**
int orientation = getResources().getConfiguration().orientation;
savedInstanceState.putInt(ORIENTATION_KEY, orientation);
}
}
There are 2 issues with the code
- Too late - When landscape child
Activityis launched, and parentActivity'sonSaveInstanceStateis being called, obtained orientation value is always landscape, although parentActivityis in portrait mode originally. - Orientation is completely detached from Accelerometer - Once
setRequestedOrientationis being called, parentActivityorientation will be fixed, and no longer depend on device Accelerometer. My intention is to restore parentActivityinitial orientation after childActivityhad quit. After that, we are still free to play around with parentActivityorientation, by rotating the device.