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
Activity
is launched, and parentActivity
'sonSaveInstanceState
is being called, obtained orientation value is always landscape, although parentActivity
is in portrait mode originally. - Orientation is completely detached from Accelerometer - Once
setRequestedOrientation
is being called, parentActivity
orientation will be fixed, and no longer depend on device Accelerometer. My intention is to restore parentActivity
initial orientation after childActivity
had quit. After that, we are still free to play around with parentActivity
orientation, by rotating the device.