Android - 在横向和纵向模式之间切换使 Intent 失去价值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/584779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:19:00  来源:igfitidea点击:

Android - switching between landscape and portrait mode makes Intent lose values

androidandroid-orientationlandscape-portrait

提问by lostInTransit

I am using Intents to switch between activities in my Android app. I am putting data in the Intent for use in the next activity. When I switch the phone between landscape and portrait modes, the values passed from the intent are lost and I get a NullPointerException.

我正在使用 Intents 在我的 Android 应用程序中的活动之间切换。我将数据放入 Intent 以供下一个活动使用。当我在横向和纵向模式之间切换手机时,从意图传递的值会丢失,并且我收到 NullPointerException。

Can someone please tell me what could be wrong.

有人可以告诉我什么可能是错的。

There's a lot of code to post it entirely. But if someone needs to look at specific parts of code, I can post it here.

有很多代码可以完全发布它。但是如果有人需要查看代码的特定部分,我可以在这里发布。

Edit
I solved the issue of state not being saved. But another problem I faced is that none of the buttons on the screen work after the orientation has been changed. On button press, I get this warning in LogCat

编辑
我解决了状态未被保存的问题。但我面临的另一个问题是,在更改方向后,屏幕上的任何按钮都不起作用。按下按钮时,我在 LogCat 中收到此警告

02-25 23:07:49.190: WARN/WindowManager(58): No window to dispatch pointer action 0

Please help.

请帮忙。

回答by Quintin Robinson

When you switch orientation the activity is recreated and onCreate is recalled so you have to use the bundle to save your current state and restore after an orientation change. You can see this in action if you have just an app with a TextView and you enter text and change orientation. If you bundle your state for onCreate you can curb this. This is probably also why you have a NullPointer after the orientation changes. It is annoying as all hell but something we have to live with.

当您切换方向时,将重新创建 Activity 并调用 onCreate,因此您必须使用 bundle 来保存当前状态并在方向更改后恢复。如果您只有一个带有 TextView 的应用程序并且您输入文本并更改方向,您可以看到这一点。如果您为 onCreate 捆绑您的状态,您可以遏制这种情况。这可能也是在方向改变后你有一个 NullPointer 的原因。它令人讨厌,但我们必须忍受。

Thislink on a series of orientation tutorials and this first onein particular should help you understand exactly what is going on and how to successfully maintain your current state.

这个关于一系列定向教程的链接,特别是第一个教程,应该可以帮助您准确了解正在发生的事情以及如何成功地保持当前状态。

Update: There is also a post on SO Activity restart on rotation Androidthat deals with almost the same thing.

更新:还有一篇关于 SO Activity restart on rotation Android的帖子,处理几乎相同的事情。

Edit for follow up question:

编辑后续问题:

Did you re-attach your click handlers after the orientation change?

您是否在方向更改后重新附加了点击处理程序?

回答by Hulk

Write this in your manifest file..in which activity you want this--

将此写入您的清单文件中..在您想要的活动中 -

 android:configChanges="orientation|keyboardHidden"

Edited:Use this one for new APIs versions--

编辑:将此用于新的 API 版本--

android:configChanges="orientation|keyboardHidden|screenSize"

Definitely it will work..

肯定会奏效的。。

回答by Hulk

Try this:

尝试这个:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(SOME_KEY, "blah blah blah");
}

@Override
public void onCreate(Bundle savedInstanceState) {
   ...
   somevalue = savedInstanceState.getString(SOME_KEY);
   ...
}

回答by Nadav Daniel

It possible to declare an attribute android:configChanges with the value of "orientation", this will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

可以声明一个属性 android:configChanges 的值为“orientation”,这将阻止活动重新启动。相反,活动保持运行并调用其 onConfigurationChanged() 方法。

回答by Nguyen Minh Binh

Declare < android:configChanges="orientation|keyboardHidden"/>in your manifest. This allows you manage the change of Orientation/Keyboard visibility by yourself. Of course, You don't need to override the callback method for manage it.

< android:configChanges="orientation|keyboardHidden"/>在您的清单中声明。这允许您自己管理方向/键盘可见性的变化。当然,您不需要覆盖回调方法来管理它。

回答by Noyloy

Hi I also encountered this problem. what fixed it for me was:

你好我也遇到了这个问题。为我修复的是:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putString("Username", mUsername);
    savedInstanceState.putString("Password", mPassword);
    savedInstanceState.putString("UserID", mUserID);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

and then in onCreate():

然后在 onCreate() 中:

if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        mUsername = "?";
        mPassword = "?";
        mUserID = "?";
    } else {
        mUsername = extras.getString("Username");
        mPassword = extras.getString("Password");
        mUserID = extras.getString("UserID");
    }
} else {
    mUsername = (String) savedInstanceState.getSerializable("Username");
    mPassword = (String) savedInstanceState.getSerializable("Password");
    mUserID = (String) savedInstanceState.getSerializable("UserID");
}

then you can be sure the objects are not null.

那么您可以确定对象不为空。