Deepa MG Deepa MG - 9 days ago 9
Android Question

what is difference between onCreateView() and getView(),Can I use these in Activity()?

I am confused about usage of

onCreate()
,
onCreateView()
and
getView()
.

1. Can i use all these three in my
activity
class?

2. Should i compulsorily have different
layouts
for each?

Answer

Here is description o these methods from Google Developer website:

onCreate()

It gets 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.

OnCreateView()

It is not a lifecycle method for activity. It's just a member method which will be used for specified tasks as said in doc.

Standard implementation of android.view.LayoutInflater.Factory.onCreateView used when inflating with the LayoutInflater returned by getSystemService. This implementation does nothing and is for pre-android.os.Build.VERSION_CODES.HONEYCOMB apps. Newer apps should use onCreateView(View, String, Context, AttributeSet). To rely of call of onCreateView() in Activity is bad programming.

If you are using this method for Fragments then,

It will be Called to have the fragment instantiate its user interface view.

getView()

This method is available for Fragments only.

It gets the root view for the fragment's layout (the one returned by onCreateView(LayoutInflater, ViewGroup, Bundle)), if provided.

References:

https://developer.android.com/reference/android/app/Fragment.html#getView() https://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)