373

What are the differences between onCreate(), onCreateView(), and onActivityCreated() in fragments and what would they each be used for?

Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
  • See also: http://stackoverflow.com/questions/27227906/where-is-the-diferent-between-oncreate-and-oncreateview – Brad Larson Sep 18 '15 at 14:56
  • 2
    @BradLarson I don't understand why this had been closed. It has proved to be a relatively popular question and is different to the link stated in your comment. This question is asking for the difference between the three different methods and how they compare to each other but the question you linked in your comment only mentions two of these methods. – Farbod Salamat-Zadeh Sep 18 '15 at 15:11
  • @BradLarson Fair enough and well spotted. Now though that I have an understanding, could I not write an answer which better compares the three methods, referencing the links for additional details? – Farbod Salamat-Zadeh Sep 18 '15 at 15:37
  • 3
    @FarbodSalamat-Zadeh - Sure. I've reopened the question, if you think you can provide a better answer. I just didn't want to leave it sitting unanswered if I could. – Brad Larson Sep 18 '15 at 15:52

3 Answers3

448

UPDATE:

onActivityCreated() is deprecated from API Level 28.


onCreate():

The onCreate() method in a Fragment is called after the Activity's onAttachFragment() but before that Fragment's onCreateView().
In this method, you can assign variables, get Intent extras, and anything else that doesn't involve the View hierarchy (i.e. non-graphical initialisations). This is because this method can be called when the Activity's onCreate() is not finished, and so trying to access the View hierarchy here may result in a crash.

onCreateView():

After the onCreate() is called (in the Fragment), the Fragment's onCreateView() is called. You can assign your View variables and do any graphical initialisations. You are expected to return a View from this method, and this is the main UI view, but if your Fragment does not use any layouts or graphics, you can return null (happens by default if you don't override).

onActivityCreated():

As the name states, this is called after the Activity's onCreate() has completed. It is called after onCreateView(), and is mainly used for final initialisations (for example, modifying UI elements). This is deprecated from API level 28.


To sum up...
... they are all called in the Fragment but are called at different times.
The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView(). Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed.


If you want to view the official Android documentation, it can be found here:

There are also some slightly different, but less developed questions/answers here on Stack Overflow:

Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
  • 7
    I thought I would implement non graphical initialisations at onCreate() so that they would not be called again when the screen is rotated. It turns out that I have to call fragment.setRetainInstance(true) otherwise both onCreate() and onCreateView() are called again when the screen is rotated. – Damn Vegetables Feb 13 '16 at 04:52
  • In onCreateView(), is that safe to access view hierarchy ? – Cody Apr 11 '16 at 06:23
  • @Cody I believe so - accessing the view hierarchy is the exact purpose of `onCreateView`. – Farbod Salamat-Zadeh Apr 11 '16 at 16:32
  • 1
    However, activity's onCreate() might not finished until onActivityCreated() ? Is these any chance of crash in onCreateView for accessing view hierarchy ? I am not sure what's difference between onCreateView() / on onActivityCreated() – Cody Apr 11 '16 at 23:45
  • @Cody Read my answer in more detail; `onCreate` is called first, then `onCreateView`, then `onActivityCreated`. There should be no crashes if you are correctly accessing the view hierarchy in `onCreateView` or `onActivityCreated` - it doesn't matter which, but usually, graphical initialisations are done in `onCreateView`. – Farbod Salamat-Zadeh Apr 12 '16 at 16:57
  • 1
    One thing to note (at least with the AppCompatActivity) is that when the activity is recreated (e.g. after being minimised and killed) the fragments onCreate() will be called before the the activities onCreate() and super.onCreate() are finished. This can be a problem if you are using something like Dagger and need to access something in the parent activity that is injected. One solution to this is to put the code in onActivityCreated() which is called always called of onCreate() is called. – Nicholas Jul 10 '17 at 00:06
  • If i need to get some data from DB, process it and create graph (or maybe adapters). should i do that in onCreate (just process data from db), onCreateView , onActivityCreated or onStart ? – razor Sep 27 '17 at 16:17
  • @razor I don't think it makes a huge difference, `onCreate` is probably best but it depends on when you want the processing to be done. Just keep in mind that you can't access the view hierarchy in `onCreate`. – Farbod Salamat-Zadeh Sep 27 '17 at 20:05
  • 1
    Is it save to assume that the activity's `onCreate` has finished, once the `onCreateView` of it's Fragments is called? – nullmn Dec 31 '19 at 16:48
  • There is pretty much no need to use `onActivityCreated`, ever, and just use `onViewCreated` instead. – EpicPandaForce Sep 28 '20 at 05:46
170

For anyone looking for a concise, pictorial answer:

enter image description here https://hanaskuliah.wordpress.com/2015/12/07/android-5-development-part-6-fragment/


And,

enter image description here

Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
13

onActivityCreated() - Deprecated

onActivityCreated() is now deprecated as Fragments Version 1.3.0-alpha02

The onActivityCreated() method is now deprecated. Code touching the fragment's view should be done in onViewCreated() (which is called immediately before onActivityCreated()) and other initialization code should be in onCreate(). To receive a callback specifically when the activity's onCreate() is complete, a LifeCycleObserver should be registered on the activity's Lifecycle in onAttach(), and removed once the onCreate() callback is received.

Detailed information can be found here

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77