Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments
Asked Answered
D

3

375

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

Decretory answered 8/3, 2015 at 17:33 Comment(4)
See also: #27228406Sprite
@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.Decretory
@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?Decretory
@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.Sprite
D
452

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:

Decretory answered 18/9, 2015 at 16:25 Comment(10)
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.Flofloat
In onCreateView(), is that safe to access view hierarchy ?Gamez
@Gamez I believe so - accessing the view hierarchy is the exact purpose of onCreateView.Decretory
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()Gamez
@Gamez 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.Decretory
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.Interferometer
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 ?Delmore
@Delmore 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.Decretory
Is it save to assume that the activity's onCreate has finished, once the onCreateView of it's Fragments is called?Chansoo
There is pretty much no need to use onActivityCreated, ever, and just use onViewCreated instead.Afterburning
C
171

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

Calvo answered 16/6, 2017 at 6:39 Comment(4)
Agreed. Comic Sans is necessary for things like thisRemonstrant
It's the first time I see 3 different fonts in the same diagram, and somehow my life feels complete now.Strength
When exactly could a fragment restart?Turgor
How come the fragment is not re-created once it's process is dead? AFAIK, only the task state preserved as a bundle and it's re-created the app is brought to foreground again.Manakin
H
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

Hera answered 28/3, 2020 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.