AndyAndroid
AndyAndroid

Reputation: 4069

Android: Bundle always null

I have a fragment and for screen rotation I want to save some states I have

@Override
public void onSaveInstanceState(Bundle bundle)
{
    super.onSaveInstanceState(bundle);
    bundle.putSerializable("myList", myList);
    bundle.putString("test", "test");
 }

to save the data I need, I can see in the debugger that the code is at least called and then to get the data I have

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    super.onActivityCreated(savedInstanceState);

    if(savedInstanceState != null)
    {
        //do something        
    }
    else
    {
        //do something else
    }
}

but here I always end up the else-brach of my if-statement. I have no idea why it is null. Thanks.

EDIT changed the code according to the first answer plus:

When rotating the screen I see in the debuger this:

  1. onSaveInstanceState is called
  2. onCreate is called --> bundle != null
  3. onCreate is called again (Why?) --> bundle = null
  4. onCreateActivity is called --> bundle = null

EDIT 2 I found simlar posts about tabs, where they got detached and that is why it is called twice. I have to admit I haven't fully understood these posts... but it could be related to that. In my Activity I have the following code

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    OverviewFragment of = new OverviewFragment();
    FragmentTransaction tof = getFragmentManager().beginTransaction();

    tof.replace(R.id.frag_overview, of);
    DetailFragmentInitial df = new DetailFragmentInitial();
    tof.replace(R.id.frag_details, df);
    tof.commit();

}

and might have to change that somehow... but I am not sure... If I perform the fragment transaction only if the savedInstaceState is null then it seems to work. But I am not sure if I run into a different issue later. Someone some background knowledge on this?

Upvotes: 1

Views: 4264

Answers (1)

Lukas Knuth
Lukas Knuth

Reputation: 25755

For better visibility, here here is the working solution for @AndyAndroid (as outlined in his comment):

The answer is to suround my fragment transaction in Edit 2 with if(getFragmentManager().findFragmentByTag("overviewFrag") == null) and of course set the tag.


There is an older question on the same topic, which can be found here: Saving Android Activity state using Save Instance State

To outline the difference between the code (in the accepted answer) and yours, the example calls the super-method before putting it's own values into the bundle.

The comment from @jkschneider outlines this:

CAREFUL: you need to call super.onSaveInstanceState(savedInstanceState) before adding your values to the Bundle, or they will get wiped out on that call (Droid X Android 2.2).


As a general advice, always annotate overwritten methods with the @Override-annotation, to get compile-time security. For more information, see this: When do you use Java's @Override annotation and why?


Here are some more related questions which might help:

Check if the code given in the last answer works for you, as it seems that it does for the OP of the other two questions.

Upvotes: 3

Related Questions