【问题标题】:Android Screen Orientation Change Causes App To QuitAndroid 屏幕方向更改导致应用退出
【发布时间】:2011-02-28 22:18:34
【问题描述】:

我有一个应用程序,我通过设置将其设置为仅以纵向模式显示

android:screenOrientation="portrait"

对于清单中的每个活动。这很好用,当我旋转手机时,我的应用屏幕不会旋转。

问题是,如果我用手机以纵向方向启动应用程序,那么当我的第一个 Activity 运行(动画启动屏幕)并且我在第一个 Activity 仍在运行时将手机旋转到横向,当下一个 Activity启动(它从第一个活动线程的 finally 块启动)应用程序将退出。它不会强制退出,它只是退出到主屏幕。 LogCat 中没有抛出异常或任何其他说明发生这种情况的原因。屏幕从不旋转(因为它不应该)。如果我在横向模式下使用手机启动应用程序,它可以正常工作。这只发生在我从纵向开始然后旋转到横向时。

我试过放

android:configChanges="orientation"

在每一个活动中,我也尝试过

android:configChanges="orientation|keyboard|keyboardHidden|screenLayout"

我尝试了上述方法,无论是否在活动中实现 onConfigurationChanged。当我实现它时,我只是忽略了它(只是调用 super.onConfigurationChanged)。

除了在清单中设置之外,我还尝试以编程方式将活动设置为纵向。

我也可以通过在物理键盘打开的情况下启动应用程序并在车载底座中启动应用程序来在原始摩托罗拉 Droid 上实现这一点。应用程序启动,启动画面以纵向启动(即使键盘打开或手机在车载底座中),但一旦启动第二个活动,应用程序就会退出。

我认为这与 Activity 在方向更改时被破坏没有任何关系。我所有的全局数据都存储在我的应用的自定义 Application 类中,正如我之前提到的,在全局被清除的情况下,我没有遇到任何异常,例如空指针。

我正在使用 Android 2.3.2 在 Verizon 上运行摩托罗拉 Droid 原版。

【问题讨论】:

  • 您是说当您强制纵向时,活动仍会旋转到横向?

标签: android rotation landscape screen-orientation screen-rotation


【解决方案1】:

我建议以编程方式设置初始屏幕内的方向或下一个活动。 尝试实现这段代码:

//get the WindowManager Service
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
//get the orientation
int orientation = display.getOrientation();
//orientation % 2 == 0 is portrait mode,
if ((orientation % 2) != 0) (android.view.IWindowManager.Stub.asInterface(ServiceManager.getService("window"))).setOrientation(0);

我希望这会奏效。

【讨论】:

    【解决方案2】:

    应用程序在方向更改时关闭,因为您必须在 res 文件夹中提供特定资源

    • 例如:layout-land 在 res 文件夹中 或

    通过编程方式在运行时处理更改

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多