what is the different between onCreate() and onCreateView() lifecycle methods in Fragment?
Asked Answered
S

3

52

I don't know when to use onCreate() or onCreateView().

I have used onCreate() and onCreateView() lifecycle methods. I think onCreate() for Activity and onCreateView() for Fragment. But I am not sure. Can I use onCreate() LifeCycle method in Fragment? I hope somebody can help me!

Significancy answered 1/12, 2014 at 11:57 Comment(3)
check the android developer docs and maybe you will find a solutionOvermodest
have you read fragments lifecycle?Healing
Google - google.co.in/…Squab
T
85

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. It is always called some time after the onCreate method.

Trudy answered 1/12, 2014 at 11:59 Comment(0)
M
8

Activity lifecycle explained - http://developer.android.com/reference/android/app/Activity.html

Fragment lifecycle explained - http://developer.android.com/guide/components/fragments.html#Creating

Detailed lifecycle diagram - https://github.com/xxv/android-lifecycle

Martineau answered 1/12, 2014 at 12:2 Comment(1)
Simple links don't answer questions. For instance, in the lifecycle there's no mention of the createView.Elatia
C
0

From documents :

onCreate

Called when the activity is starting.

This is where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById(int) to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri, String[], String, String[], String) to retrieve cursors for data being displayed, etc.

You can call finish() from within this function, in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

Link to documentation of onCreate

onCreateView

Called to have the fragment instantiate its user interface view. This is optional, and non-graphical fragments can return null (which is the default implementation). This will be called between onCreate(Bundle) and onActivityCreated(Bundle).

If you return a View from here, you will later be called in onDestroyView() when the view is being released.

Link to documentation of onCreateView

Consultant answered 1/12, 2014 at 12:2 Comment(1)
This doesn't answering the question as the onCreate() described is for Activity. The reason is simply because question is asking about Fragment. Not to mention, Fragment does not have setContentView() method.Exsert

© 2022 - 2024 — McMap. All rights reserved.