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?
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 :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With