1

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.

Dhruv
  • 17
  • 1
  • 3
  • 1
    go through http://stackoverflow.com/questions/17203490/why-does-the-fragments-oncreateview-oncreate-onactivitycreated-are-called – Vikalp Patel Jun 13 '14 at 12:07

2 Answers2

3

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

Robi Kumar Tomar
  • 3,418
  • 3
  • 20
  • 27
2
  1. onCreate() is sort of like a constructor for the Fragment class, where you normally initialize some variables. In many cases onCreate() is not overriden at all, although it is important to manually define a public empty constructor for every Fragment class.

  2. onCreateView() is the most important method to override when you a create a Fragment (at any rate a Fragment that has a UI). It is analogous to the onCreate() of an Activity: here you inflate the View of the Fragment.

  3. onActivityCreated() is called after the Activity to which the Fragment is attached has been created (i.e. the Activity's onCreate() method has returned without errors) and after the Fragment's View has been inflated.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120