8

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?

Pi Delport
  • 10,356
  • 3
  • 36
  • 50
AndyAndroid
  • 4,039
  • 14
  • 44
  • 71
  • Use search http://stackoverflow.com/questions/5525152/replacing-fragments-and-orientation-change http://stackoverflow.com/questions/7707032/illegalstateexception-when-replacing-a-fragment http://stackoverflow.com/questions/4937075/fragment-without-a-view-crashes-on-configuration-change – Georgy Gobozov Aug 02 '12 at 09:03
  • thanks I got a step ahead, see edit in original post – AndyAndroid Aug 02 '12 at 12:37

1 Answers1

3

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

curioustechizen
  • 10,572
  • 10
  • 61
  • 110