2

My app follows the screen orientation by default, but I want to add a setting to force a specific orientation. I have been able to do this using the following code in the Application class

    public void applyOrientationMode() {
    final OrientationEnum orientation = PreferencesHelper.getOrientationMode();
    if (this.orientationLifecycleCallback != null) {
        try {
            unregisterActivityLifecycleCallbacks(this.orientationLifecycleCallback);
        } catch (final Throwable t) {
            ExceptionHelper.fullLogging(t, TAG);
        }
    }


    if (orientation != null && orientation != OrientationEnum.UNLOCKED) {
        this.orientationLifecycleCallback = new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                switch (orientation) {
                    case PORTRAIT:
                        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                        break;
                    case LANDSCAPE:
                        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                        break;
                }
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        };

        registerActivityLifecycleCallbacks(this.orientationLifecycleCallback);
    }
}

This code is called from the application.onCreate() It works fine, however if I set the settings to portrait mode and open a new activity with the screen in landscape I can see th screen created in landscape mode before being recreated in the portrait mode. How I can prevent this?

Please don't use any manifest orientation solution as this is NOT what I'm trying to do

Thanks

user1026605
  • 1,633
  • 4
  • 22
  • 58
  • I think you could get some ideas from this open source project: https://github.com/ohmae/orientation-faker – Fragkiskos Mysirlakis Dec 29 '20 at 20:14
  • Why not do a search on Google? https://stackoverflow.com/questions/4675750/lock-screen-orientation-android – emandt Dec 30 '20 at 13:30
  • @emandt Did you at least read my question??? This doesn't address the issue at all – user1026605 Dec 31 '20 at 18:55
  • While the first Activity has locked orientation to Portrait it's normal that a new activity is created using the same orientation...When it is showed, and the locked orientation is released due to stop, the new activity detects screen orientation change and update itself according. To avoid this you have to MANUALLY force second activity to open in landscape instead of current orientation. – emandt Dec 31 '20 at 23:51
  • @emandt I now that and I'm asking how to do this – user1026605 Jan 01 '21 at 16:48

0 Answers0