Issue
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:
- onSaveInstanceState is called
- onCreate is called --> bundle != null
- onCreate is called again (Why?) --> bundle = null
- 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?
Solution
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:
- Fragment onCreateView and onActivityCreated called twice (The one you where talking about)
- android onCreateOptionsMenu called twice when restoring state (The same guy asked a day later and got a response from someone else)
- onCreateOptionsMenu is being called too many times in ActionBar using tabs (The link he provided)
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.
Answered By - Lukas Knuth
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.