Reputation: 42730
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
Activity
is launched, and parent Activity
's onSaveInstanceState
is being called, obtained orientation value is always landscape, although parent Activity
is in portrait mode originally.setRequestedOrientation
is being called, parent Activity
orientation will be fixed, and no longer depend on device Accelerometer. My intention is to restore parent Activity
initial orientation after child Activity
had quit. After that, we are still free to play around with parent Activity
orientation, by rotating the device.Upvotes: 2
Views: 902
Reputation: 42730
Here's my solution, which will both solve for Too late and Orientation is completely detached from Accelerometer
private int orientationBeforeHistory = Integer.MIN_VALUE;
private void launchChildActivity() {
this.orientationBeforeHistory = Utils.getScreenOrientation(this.getActivity());
...
}
@Override
public void onSaveInstanceState (Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt(ORIENTATION_BEFORE_HISTORY_KEY, this.orientationBeforeHistory);
}
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (savedInstanceState != null) {
final int orientationBeforeHistory = savedInstanceState.getInt(ORIENTATION_BEFORE_HISTORY_KEY);
if (Integer.MIN_VALUE != orientationBeforeHistory) {
final Activity activity = getActivity();
activity.setRequestedOrientation(orientationBeforeHistory);
// Key step for not locking screen orientation.
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}
}
For getting current orientation, please refer to How do I get the CURRENT orientation (ActivityInfo.SCREEN_ORIENTATION_*) of an Android device?
Upvotes: 1