2

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

  1. Too late - When landscape child 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.
  2. Orientation is completely detached from Accelerometer - Once 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.
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • You could save the orientation just before starting the child Activity. You can then restore it just after you called the finish method in the child Activity. Maybe it can do the trick. – Phantômaxx Mar 05 '14 at 15:20
  • Yes. Your suggestion will solve **Too late** problem. But, how about **Orientation is completely detached from Accelerometer** after calling `setRequestedOrientation`? – Cheok Yan Cheng Mar 05 '14 at 15:28
  • So, you aren't anymore able to rotate the device and get the corresponding screen orientation after exiting the child Activity? This is strange. it seems that there's some misconfiguration in the manifest file, but of course you already checked for that... did you? No, it can't be the issue, or you should not be able to get the orientation change even BEFORE calling the child Activit. – Phantômaxx Mar 05 '14 at 15:33
  • Yes. I do check the manifest, I do not specific any orientation for parent activity, but I do specific I want `android:screenOrientation="landscape"` for child activity. Hence, I expect parent orientation will always respect to Accelerometer. – Cheok Yan Cheng Mar 05 '14 at 15:37
  • ... let me think... maybe you have to restore (or undo) the Accelerometer sensing that you might have disabled for the child Activity. Maybe you can force the child by code instead of disblig the sensing in the manifest, becase, then, maybe, the parent becomes unresponsive to orientation changes as if the Accelerometer, once disabled, stays disabled forever (!). S, probably, a more convenient way to request the child to stay ni Landscape mode is to request the feature in... onCreate (?) - something like when you request a FullScreen and/or a NoTitle setting. (Without using a theme) – Phantômaxx Mar 05 '14 at 15:44
  • And... look what I found right now (to be added to the PARENT activity in manifest): `` note: screenOrientation="**sensor**". Also, as per the official docs, "Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "**screenSize**" configuration, because it also changes when a device switches between portrait and landscape orientations." – Phantômaxx Mar 05 '14 at 15:48
  • @ArtooDetoo No. After calling setRequestedOrientation in parent activity `onCreate`, it won't work still. `screenOrientation="sensor"` don't really help. – Cheok Yan Cheng Mar 05 '14 at 16:14
  • I finished my bullets on this argument... I really can't think of anything else that may help you. Even doing permutations and combinations of the above, it seems that the problem doesn't have a reachable valid solution for my (still little) knowledge. – Phantômaxx Mar 05 '14 at 16:20

1 Answers1

1

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?

Community
  • 1
  • 1
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875