Question

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.

Was it helpful?

Solution

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?

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top