Android 屏幕方向更改导致应用程序退出

发布于 2024-10-19 22:21:11 字数 1019 浏览 9 评论 0原文

进行设置,将其设置为仅以纵向模式显示

android:screenOrientation="portrait"

我有一个应用程序,通过为清单中的每个活动 。这工作正常,当我旋转手机时,我的应用程序屏幕不会旋转。

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

我尝试过放入

android:configChanges="orientation"

每个活动,也尝试过

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

在活动中执行或不执行 onConfigurationChanged 的​​情况下尝试上述操作。当我实现它时,我只是忽略它(只是调用 super.onConfigurationChanged)。

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

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

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

我正在 Verizon 上运行 Android 2.3.2 的 Motorola Droid 原装版。

I have an app that I set to only display in Portrait mode by setting

android:screenOrientation="portrait"

for each Activity in the manifest. This works fine and my app screen does not rotate when I rotate the phone.

The problem is, if I start the app with the phone in Portrait orientation, then as my first Activity runs (an animated splash screen) and I rotate the phone to Landscape while the first Activity is still running, when the next activity is launched (it is launched from the finally block of the first activity's thread) the app will quit. It does not force quit, it simply exits to the home screen. There are no exceptions thrown or anything else in LogCat that tells why this is happening. The screen never rotates (as it shouldn't). If I start the app with the phone in Landscape it works fine. This only happens when I start in Portrait and rotate to Landscape after.

I have tried putting

android:configChanges="orientation"

in every activity and I have also tried

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

I tried the above with and without implementing onConfigurationChanged in the activities. When I did implement it I simply ignored it (just calling super.onConfigurationChanged).

I have also tried programatically setting the activity to Portrait orientation in addition to setting it in the manifest.

I can also get this to happen on the original Motorola Droid by starting the app with the physical keyboard open and starting the app in the car dock. The app starts, splash screen starts in Portrait orientation (even though the keyboard is open or the phone is in car dock) but as soon as that second activity is started the app quits.

I don't think this has anything to do with the Activity being destroyed on an orientation change. All my global data is stored in my app's custom Application class and as I mentioned before, I get no exceptions such as a null pointer in the case of a global getting wiped out.

I am running a Motorola Droid original on Verizon with Android 2.3.2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯 2024-10-26 22:21:11

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

//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);

我希望这会起作用。

i would recommend to set the orientation inside your splash screen or the next activity programatically.
Try to implement this code:

//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);

I hope this will work.

情话墙 2024-10-26 22:21:11

应用程序在方向更改时关闭,因为您必须在 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();
    }
}

The application closes on orientation change because you must provide with particular resources in the res folder

  • Eg:layout-land
    in res folder
    or

handle the change at runtime through programmatically

@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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文