17

I have a specific use case where I want a fragment to be locked in portrait mode, but still rotate the activity (and/or other fragments visible in the same activity). Is it possible to do that?

All the solutions to locking a fragment orientation suggest to use setRequestedOrientation and lock the activity orientation, but I need other visible fragments to rotate.

My app supports API 10+ (if there is a nice solution that uses API 11+ I may consider removing support for landscape in API <11).

Thanks in advance.

Kathy
  • 263
  • 1
  • 2
  • 7

2 Answers2

11

Take a look at this answer:

Override setUserVisibleHint() in each fragment.

In the portrait only fragments:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser) {
        Activity a = getActivity();
        if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

in the the portrait/landscape fragment:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser) {
        Activity a = getActivity();
        if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
    }
}

This will allow the whole activity to rotate in one fragment, but fix it to portrait in others.

Answered by: https://stackoverflow.com/a/13252788/2767703

Community
  • 1
  • 1
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76
  • No, this will not work. I want the activity to rotate, just not the fragment. setRequestOrientation locks the activity orientation, which I already said in my question isn't what I want. – Kathy Dec 04 '13 at 17:25
  • 3
    Then it is not possible, you cannot just rotate the activity without the fragment. You can just lock the orientation at certain fragments. – Kevin van Mierlo Dec 05 '13 at 08:04
  • The `portrait only fragments` snipet is making calling onResume() twice when app starts on landscape mode. Tested on HTC One – Ricardo Sep 13 '15 at 20:33
  • Please note that SCREEN_ORIENTATION_FULL_SENSOR will ignore user settings and will always rotate the screen depending on sensor position. If you want the user rotation settings to apply use SCREEN_ORIENTATION_UNSPECIFIED instead – Synx Feb 21 '21 at 22:04
8

Its may be little late for the reply, but as i can see you haven't found a solution, You can try doing this, Whenever you are calling your fragment from your activity, add below code before it

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

and for all other/default Fragment

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

Hope i helped

Bikash
  • 1,452
  • 1
  • 15
  • 24
  • 1
    This is working fine. But when back button is pressed the previous fragments are also in changed orientation. SO how can we stop previous fragments from changing their orientation? – Farrakh Javed Sep 05 '16 at 10:19
  • 1
    Write one Base fragment and apply what @bikash wrote in it. Then make all your other fragments inherit from this one, to propagate the behavior. – Maxime Claude Aug 03 '17 at 16:31