I have started working on fragments.Before starting i wanna clear a doubt whether when to use onActivitycreate() and onCreate().I did many research but cound not find any relevent answer.Please help.
-
1go through http://stackoverflow.com/questions/17203490/why-does-the-fragments-oncreateview-oncreate-onactivitycreated-are-called – Vikalp Patel Jun 13 '14 at 12:07
2 Answers
onActivitycreate() :
onActivitycreate()
Called when the fragment's activity has been created and this fragment's view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. It is also useful for fragments that use setRetainInstance(boolean) to retain their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onViewStateRestored(Bundle).
onCreate():
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
Read this doc for more details: Fragment Activity Life cycle

- 3,418
- 3
- 20
- 27
onCreate()
is sort of like a constructor for theFragment
class, where you normally initialize some variables. In many casesonCreate()
is not overriden at all, although it is important to manually define a public empty constructor for everyFragment
class.onCreateView()
is the most important method to override when you a create aFragment
(at any rate aFragment
that has a UI). It is analogous to theonCreate()
of anActivity
: here you inflate theView
of theFragment
.onActivityCreated()
is called after theActivity
to which theFragment
is attached has been created (i.e. theActivity
'sonCreate()
method has returned without errors) and after theFragment
'sView
has been inflated.

- 30,051
- 12
- 94
- 120