Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 628
Farbod Salamat-Zadeh Avatar asked Mar 08 '15 17:03

Farbod Salamat-Zadeh


People also ask

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

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.

What is difference between onCreate and onCreateView in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.

What is onActivityCreated in fragment?

onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity. onCreate() . onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.

What does the onCreateView () method return if a fragment doesn't have any UI?

These files contain only the onCreateView() method to inflate the UI of the fragment and returns the root of the fragment layout. If the fragment does not have any UI, it will return null.


2 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:

  • onCreate()
  • onCreateView()
  • onActivityCreated()_

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

  • onCreate() vs onCreateView()
  • onCreateView() vs onActivityCreated()
like image 167
Farbod Salamat-Zadeh Avatar answered Sep 22 '22 00:09

Farbod Salamat-Zadeh


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

like image 31
Manish Kumar Sharma Avatar answered Sep 24 '22 00:09

Manish Kumar Sharma