Android: fragment life cycle when rotating the screen

I know the number 2 http://developer.android.com/guide/components/fragments.html I wonder what comes from "Fragment Active" when I rotate the screen and finally return to "Fragment Active".

The story of my question is that I have an application that works great, regardless of whether I start it in portrait or landscape mode. But when the screen rotates, it unloads

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

This snippet has basically only implemented onCreateView, nothing else

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

Knowing what exactly happens when the screen is rotated, I hope to solve the problem ...

EDIT:

I tried what the commentator suggested, and some more information about it. Thus, all of them basically offer to have an empty action layout and add fragments programmatically, if I see it correctly. I have main.xml for the portrait and one for the landscape, both look very similar (the difference is horizontal and 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 is as follows:

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

It seems that the screen rotation works with it (so thanks so far!), But in the landscape I see only the first fragment in the portrait, which I see both the second and second time (the more I rotate, the more often they are added) , So either I have a problem with the layout, or I can’t add some fragments like this. Still trying to figure out if this is a layout issue, but there is no clue yet. Any hint?

+8
android android-fragments
source share
1 answer

As I understand your problem, you should not add snippets every time. You must replace what is currently 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 of the Activity destroyed and recreated; therefore, everything that is needed until onDetach() is called follows everything from onAttach() .

A harsh way to know is to override all life cycle methods and put a log message in it :-)

+3
source share

All Articles