Search code examples
androidorientationscreen-orientationandroid-orientationonconfigurationchanged

Android Ignore configuration change


A requirement of my application is that it must stay in portrait orientation at all times except when the user is viewing media.

In my manifest I have set android:configChanges="orientation|screenSize" so onConfigurationChanged gets called each time the user changes from portrait to landscape.

I have implemented a youtubeplayersupportfragment in my app. When the user selects full screen I then want to allow the user to rotate the screen to landscape, in order to allow the user to view in fullscreen if required.

The Youtubeplayer has a fullscreen listener which I have implemented here

YouTubePlayer.OnInitializedListener YoutubePlayerInitializer = new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
                youTubePlayer.cueVideo(new MediaUtils().getVideoId(mFeedContent.getMedia().getMediaUrl()));
                youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                youTubePlayer.setFullscreenControlFlags(2);

                youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
                    @Override
                    public void onFullscreen(boolean b) {
                        if(b)
                            PreferenceUtils.setDisplayMedia(b);
                        else PreferenceUtils.setDisplayMedia(b);
                    }
                });
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult){
                Log.i(AbstractHolder.class.getSimpleName(), String.valueOf(youTubeInitializationResult));
            }
        };

My question is: Is there anyway to ignore the config changes based on circumstances i.e. 'b' in the code below equals false?

There is no point in calling the method setRequestedOrientation() as this permanently fixes the orientation and onconfigurationchanged doesn't get called again which I need called when orientation is changed.

 @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        boolean media = PreferenceUtils.getDisplayMedia();

        if (newConfig.orientation == newConfig.ORIENTATION_LANDSCAPE && media) {
            //ALLOW ROTATION
            L.i(MainActivity.class.getSimpleName(), "==================================LANDSCAPE");
        } else {
            //IGNORE CONFIGURATION CHANGE
            L.i(MainActivity.class.getSimpleName(), "==================================PORTRAIT");
        }
    }

Solution

  • You should use another approach

    simply set this to lock the orientation as Portrait if not playing youtube

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
    

    when switch to playing youtube, set back to allow rotation with this command

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR)