In a Fragment's Lifecycle, the onAttach() method is called before the onCreate() method. I can't wrap my head around this. Why would you attach a Fragment first?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
TL;DR:
In order to not break the design consistency amongst different UI components in android,the
onCreate()
method will have similar functionality across all of them.When linking Containers to Contents like Window to Activity and Activity to Fragment a preliminary check needs to be done to determine the state of container. And that explains the use and position of
onAttach()
in the fragment lifecycle.Too short;Need longer:
The answer is in the archetype code itself,
Another example would be in Jake Wharton's ActionBarSherlock library.
Why would you want to use a method like
onCreate()
which is has the same purpose in an activity ,service.The
onCreate()
is meant to handle issues with respect to that particular context creation.It does not make sense ifonCreate()
is used to check the state of its container.The second reason that I can come determine is that a fragment is designed to be activity independent.The
onAttach()
provides an interface to determine the state/type/(other detail that matters to the fragment) of the containing activity with reference to the fragment before you initialize a fragment.EDIT:
An activity exists independently and therefore has a self sustaining lifecycle.
for a fragment :
The independent lifecycle components(same as any other components):
The interaction based components:
from the documentation:
answering another question which came up in the comments:
The design philosophy states that a Fragment is designed for reuse. A fragment (by design) could(and should) be used across multiple activities.
The
onCreate
by definition is responsible to create a fragment. Consider the case of orientation,your fragment could be: - using different layouts in different orientations. - applicable only in portrait orientation and not landscape - To be used only on tables and mobile phones.All these situations would require a check before the fragment is initialized from the android perspective(
onCreate()
) and the view inflated(onCreateView()
).Also consider the situation of a headless fragment.The
onAttach()
provides you the interface required for preliminary checks.Because
onAttach()
assigns hosting activity to theFragment
. If it had been called afteronCreate()
then there would be no context for your fragment (getActivity()
would returnnull
) and you would not be able to do anything inonCreate()
method without that context anyway.Another fitting reason is that Fragment's lifecycle is similar to Activity's lifecycle. In
Activity.onAttach()
activity gets attached to its parent (a window). Similarly inFragment.onAttach()
fragment gets attached to its parent (an activity), before any other initialization is done.This is related to retained fragments. Following Fragment
setRetainInstance(boolean retain)
documentation:Take a look at the source code (android.support.v4.app.FragmentManager, v21):
Example
Case 1: not retained fragment or setRetainInstanceState(false)
Application is started. Fragment is added dynamically using FragmentManager or inflated from XML via
setContentView()
.onAttach()
called after Activitysuper.onCreate()
call - Activity is already initialised.Configuration changed. Activity recreates fragments from saved state, fragments are added/attached from inside Activity
super.onCreate()
call:Case 2: setRetainsInstanceState(true)
Application is started. Fragment is added dynamically using FragmentManager or inflated from XML via
setContentView()
. Same as above:onAttach()
called after Activitysuper.onCreate()
call - Activity is already initialised.Configuration changed.
Fragment
onCreate()
not called, butonAttach()
still called - you need to know, that hosting Activity has changed. But still fragment is already created, so noonCreate()
called.Two Points from Android developer Site hints at why
onAttach()
is called beforeonCreate()
in case of Fragment Life cycle.Also, When you add a fragment as a part of your activity layout, it lives in a ViewGroup inside the activity's view hierarchy.
So Fragment must FIRST "attach" itself to activity to defines its own view layout
onCreate
is Called to do initial creation of a fragment.It is obvious that you will create something only when its pre-condition of creation is in place (and the pre-condition is A fragment must always be embedded in an activity, and it must be attached to its Activity)