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

6

20

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.

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

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

Please help.

Enharmonic answered 25/2, 2009 at 5:10 Comment(0)
T
25

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?

Tessi answered 25/2, 2009 at 5:23 Comment(3)
Why the last question? Is it important to re-attach listeners? Please let me know. :)Selfassertion
Yes, because if you don’t re-attach them they will still be attached to the old buttons which don’t exists anymore.Whaleboat
+1 thanks! I just got stuck with this error for the last hour. I have come to really detest Android Development! It complicates trivial things.Gibson
F
9

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..

Frisbee answered 10/5, 2012 at 9:17 Comment(2)
This was worked in emulator. But didn't work in the real device.Dues
This will also work on real device. Make sure you have added this on the activity on which the problem occurs.Frisbee
Z
5

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);
   ...
}
Zofiazoha answered 13/7, 2009 at 9:33 Comment(0)
E
3

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.

Escadrille answered 20/3, 2011 at 14:21 Comment(1)
Add this is to the activity tagLifesize
B
1

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.

Babb answered 22/10, 2012 at 6:3 Comment(0)
S
0

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.

Schreibman answered 29/10, 2015 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.