What are the differences between onCreate()
, onCreateView()
, and onActivityCreated()
in fragments and what would they each be used for?
-
See also: stackoverflow.com/questions/27227906/…– Brad LarsonCommented Sep 18, 2015 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-ZadehCommented Sep 18, 2015 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-ZadehCommented Sep 18, 2015 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 LarsonCommented Sep 18, 2015 at 15:52
3 Answers
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:
-
7I 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. Commented Feb 13, 2016 at 4:52
-
-
1However, 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()– CodyCommented Apr 11, 2016 at 23:45
-
1One 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.– NicholasCommented Jul 10, 2017 at 0:06
-
1Is it save to assume that the activity's
onCreate
has finished, once theonCreateView
of it's Fragments is called?– nullmnCommented Dec 31, 2019 at 16:48
For anyone looking for a concise, pictorial answer:
https://hanaskuliah.wordpress.com/2015/12/07/android-5-development-part-6-fragment/
And,
-
36
-
4It's the first time I see 3 different fonts in the same diagram, and somehow my life feels complete now.– Gil SandCommented Feb 8, 2019 at 13:10
-
-
1How 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.– stdoutCommented Feb 29, 2020 at 21:16
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