376

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

4
  • See also: stackoverflow.com/questions/27227906/… Commented 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. Commented 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? Commented 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. Commented Sep 18, 2015 at 15:52

3 Answers 3

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:

10
  • 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. Commented Feb 13, 2016 at 4:52
  • In onCreateView(), is that safe to access view hierarchy ?
    – Cody
    Commented Apr 11, 2016 at 6:23
  • 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
    Commented Apr 11, 2016 at 23:45
  • 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
    Commented Jul 10, 2017 at 0:06
  • 1
    Is it save to assume that the activity's onCreate has finished, once the onCreateView of it's Fragments is called?
    – nullmn
    Commented Dec 31, 2019 at 16:48
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

4
  • 36
    Agreed. Comic Sans is necessary for things like this Commented Jan 26, 2018 at 3:19
  • 4
    It's the first time I see 3 different fonts in the same diagram, and somehow my life feels complete now.
    – Gil Sand
    Commented Feb 8, 2019 at 13:10
  • When exactly could a fragment restart?
    – Saurabh
    Commented Feb 14, 2020 at 15:40
  • 1
    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.
    – stdout
    Commented Feb 29, 2020 at 21:16
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.