Take the 2-minute tour ×

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...


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):


<LinearLayout xmlns:android="http:// and so on" 

The onCreate method of my activity looks like this:


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);

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?

share|improve this question
thanks I got a step ahead, see edit in original post –  AndyAndroid Aug 2 '12 at 12:37

1 Answer 1

up vote 2 down vote accepted

The way I understand you problem, you shouldn't be adding fragments every time. You should be replacing what's currently there with your new fragment.

ft.replace(R.id.myContainer1, f1);
//and I need a second fragment
Fragment2 f2 = newFragment2();
ft.replace(R.id.myContainer2, f2);

As for the Fragment life-cycle - when you rotate the screen, the hosting Activity is destroyed and re-created; so everything right until onDetach() should be called followed by everything starting with onAttach().

Surest way to know would be to override all the life cycle methods and put in a log message in all of them :-)

share|improve this answer

Your Answer


