Android: Fragment lifecycle on screen rotation

Viewed 10250

I know figure 2 of http://developer.android.com/guide/components/fragments.html I wonder what happens from "Fragment Active" when I rotate the screen and eventually be back in "Fragment Active".

Background of my question is that I have an app which works fine regardless if I start it in portrait or landscape mode. But on screen rotation it dumps

Fragment com.bla.bla did not create a view.

This fragment has basically only onCreateView implemented, nothing else

public View onCreateView(LayoutInflater i, ViewGroup c, Bundle s)
{
   return i.inflate(R.layout.mylayout, c, false);
}

Knowing what exactly happens on screen rotation I hope to solve the issue...

EDIT:

I tried what the commenter suggested, some more information on that. So they all basically suggest to have an empty activity layout and add the fragments programmatically if I see that correctly. I have a main.xml for portrait and one for landscape, both look now very similar (difference is horizontal vs. vertical):

main.xml:

<LinearLayout xmlns:android="http:// and so on" 
    android:layout_width="fill_parent" 
    android:layout_heigt="wrap_content" 
    android:orientation=vertical" 
    android:id="@+id/myContainer">
</LinearLayout>

The onCreate method of my activity looks like this:

super.onCreate(savedInstanceBundle);
setContentView(R.layout.main);

Fragment1 f1 = newFragment1();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.myContainer, f1);
//and I need a second fragment
Fragment2 f2 = newFragment2();
ft.add(R.id.myContainer, f2);
ft.commit();

Screen rotation seems to work with that (so thank you so far!) but in landscape I only see the first fragment in portrait I see both, the second multiple times (the more often I rotate the more often they are added). So either I have a layout issue or I cannot add multiple fragments like this. Still trying to figure if it is a layout issue, but no clue yet. Any hint?

1 Answers
Related