Reputation: 41
Fragment's onCreate, onCreateView is called before Actvity's onCreate, when it is declared in activity XML layout.
Is it is normal? Can I develop code based on above life-cycle.
//Fragment declared in activity xml
<fragment android:name="screenfragments.SearchableFragment"
android:id="@+id/searchable_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Thanks
Upvotes: 0
Views: 537
Reputation: 2396
This odd ordering of lifecycle events is a result of your fragment being declared in the activity's XML instead of in code. You can create the fragment and add it to the activity in the onCreateView step of the activity so you can ensure that the fragment's onCreate is called after the activity's.
See: http://developer.android.com/training/basics/fragments/fragment-ui.html#AddAtRuntime
Upvotes: 0
Reputation: 7504
Yes, you can have what you have. But keep in mind that when you declare the fragment in the xml layout the fragment will always be present and you cannot remove it or replace it dynamically.
This is somewhat contrary to the purpose of fragments which were designed to be reusable components not tied to the Activity. In the case that you have mentioned, the Fragment is tied to the Activity.
To conclude, the design principle that you are using is not normal but it will work and you can design around it.
Upvotes: 1