How to control orientation changes for fragments in a FragmentActivity with multiple fragments

I wasted several hours trying to fix this problem so once again i’m paying it forward in the hope it’s helpful to someone else.

The problem is that it’s difficult to control configuration changes in a fragment activity with multiple fragments when you need fragments to behave differently.

Here is my scenario:
I have a fragment activity that displays different fragments one at a time. The first fragment displayed is not allowed to change orientation, it must be locked in portrait.
However the next fragment I show in the same activity needs to be able to change on both portrait and landscape orientations.

Sure I could have just rewritten my code to have the other fragment start in a different activity but that’s besides the point.

Anyway, what you have to do is make sure you can get configuration changes

Manifest code:




Activity code:

//
//
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		
		if(fragment!=null && fragment.isResumed()){
			//do nothing here if we're showing the fragment
		}else{
			setRequestedOrientation(Configuration.ORIENTATION_PORTRAIT); // otherwise lock in portrait
		}
		super.onConfigurationChanged(newConfig);
	}
	

The code above doesn’t work by itself. You would think so but there’s a problem where you never receive more onConfigurationChanged callbacks after you call setRequestedOrientation.

Luckily we can take advantage of the lifecycle of our fragment to help our activity get out of it’s funk.
When our fragment resumes we can do the following:

Fragment code:

//
//
@Override
	public void onResume() {
		getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
..}

I also make sure that my activity goes back to portrait mode if I exit this fragment.

//
//
	@Override
	public void onPause() {
		getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // set the activity back to //whatever it needs to be when going back.
		super.onPause();
	}

This ensures that the activity can go back to being stubborn about orientation changes while letting an individual fragment take over configuration changes while it’s active.
I hope this is helpful! cheers!

6 comments on “How to control orientation changes for fragments in a FragmentActivity with multiple fragments”

  1. Bill

    I don’t think this code will compile. Can you please clarify how the fragment variable in the Activity Code section is declared and initialized?

  2. vallllll

    Cool trick, i’ve been looking at this problem for a while for my camera fragment, our solution helped me. Here is how you can find your fragment without having a variable “fragment” referred in the activity:
    @Override
    public void onConfigurationChanged(Configuration newConfig) {

    if (isCameraFragmentPost()) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    super.onConfigurationChanged(newConfig);
    }

    private boolean isCameraFragmentPost() {
    Fragment fr = getSupportFragmentManager().findFragmentByTag(
    TAG_PIC_TAKING);
    return (fr != null && fr.isResumed() && fr.getClass().isInstance(
    CameraFragmentPost14.class));
    }

  3. George Baker

    Just found this and it saved me a lot of work. Thanks for the starting point on fixing this. You might want to add that with new Android versions AAPI 13 and up the screen size also changes. As a result you need to add that in you manifest code. For example android:configchanges=”orientation|screenSize”.

    Thanks again,
    George

  4. bhagyashree

    thanks a lot George.
    really helpful….

Leave a Reply

Your email address will not be published. Required fields are marked *