How to disable landscape mode in React Native Android dev mode?

后端 未结 8 609
轻奢々
轻奢々 2021-01-31 13:03

I am new to android environment. I know iOS can be done in Xcode to disable device orientation. How can I disable landscape mode or any orientation mode in React Native Android?

8条回答
  •  醉酒成梦
    2021-01-31 13:40

    You can simply the logic by adding a lifecycle callback in ReactApplication class:

    public class MyApplication extends Application{
    
    @Override
        public void onCreate() {
            super.onCreate();  
    
      registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
                @Override
                public void onActivityCreated(Activity activity, Bundle bundle) {
                    // Here we specify to force portrait at each start of any Activity
                    activity.setRequestedOrientation(
                            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
                }
    
                @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) {
    
                }
            });
    }
    

    So you don't need to configure AndroidManifest

提交回复
热议问题