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

后端 未结 3 1091
执笔经年
执笔经年 2020-12-30 19:48

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

I have used onCreate() and onCreateView() lifecycle metho

相关标签:
3条回答
  • 2020-12-30 20:09

    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

    0 讨论(0)
  • 2020-12-30 20:21

    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 sometimes after the onCreate method.

    0 讨论(0)
  • 2020-12-30 20:26

    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

    0 讨论(0)
提交回复
热议问题