简体   繁体   English

Android-屏幕方向问题-仅需要纵向

[英]Android - Screen Orientation Issue - Need only portrait

My Issue: I want my application to be run in only portrait mode. 我的问题:我希望我的应用程序只能以纵向模式运行。 For this I have to define " android:screenOrientation=portrait " in "AndroidManifest" file for each activity. 为此,我必须为每个活动在“ AndroidManifest”文件中定义“ android:screenOrientation=portrait ”。 I don't want to define that line for each activity. 我不想为每个活动定义该行。 So i came up with another solution like creating a subclass of ' Application ' and registering an activity's life cycle and in method " onActivityCreated " i have added line 因此,我想出了另一个解决方案,例如创建“ Application ”的子类并注册activity's life cycle并在方法“ onActivityCreated ”中添加了以下代码:

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

But now, problem is this "Device autorotation is enabled and currently device position is landscape and when i open my application, every activity initially launched two times." 但是现在的问题是“启用了设备自动旋转功能,并且当前设备位置处于横向状态,当我打开应用程序时,每个活动最初都启动了两次。” Is there any general solution for this? 有什么通用的解决方案吗?

Each activity is launched 2 times because it first loads in landscape mode, calls onCreate() and rotates again when it executes screen rotation line of code 每个活动被启动2次,因为它首先以横向模式加载,并在执行屏幕旋转代码行时调用onCreate()并再次旋转

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

The only way around this to mention screenOrientation="portrait" for each activity in your manifest. 解决这个问题的唯一方法是为清单中的每个活动提及screenOrientation =“ portrait”。 That is the only way you force the activity to create in the portrait mode to begin with. 这是您强制活动以纵向模式开始创建的唯一方法。

You can achieve this for your entire application by following simple way. 您可以通过以下简单方法为整个应用程序实现此目的。 This will remove the overhead of extending common base class as well as manifest declaration in each activity for Portrait. 这将消除扩展肖像基类以及在Portrait的每个活动中进行清单声明的开销。

For this your application must have a application class. 为此,您的应用程序必须具有一个应用程序类。 In its onCreate(), when your app starts for first time, you need to register an ActivityLifecycleCallbacks object (API level 14+) to receive notifications of activity lifecycle events. 在其onCreate()中,当您的应用程序首次启动时,您需要注册一个ActivityLifecycleCallbacks对象(API级别14+)以接收活动生命周期事件的通知。

Above callback give you an opportunity to execute your own code whenever any activity is started (or stopped, or resumed, or whatever). 上面的回调使您有机会在任何活动开始(或停止或恢复)时执行自己的代码。 You can use this to setRequestedOrientation() on the newly created activity. 您可以将其用于新创建的活动上的setRequestedOrientation()。

class DummyApp extends Application {

@Override
public void onCreate() {
    super.onCreate();  

    // Register Callback
    registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        }
        // Other method of the ActivityLifecycleCallbacks as well you need to over-ride.
    });

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM