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

余生颓废 提交于 2019-11-27 19:25:13
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.

This link on a series of orientation tutorials and this first one in 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 Android that deals with almost the same thing.

Edit for follow up question:

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

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

 android:configChanges="orientation|keyboardHidden"

Edited: Use this one for new APIs versions--

android:configChanges="orientation|keyboardHidden|screenSize"

Definitely it will work..

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);
   ...
}

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.

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.

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():

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!