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?