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?
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?
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:
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)
first refer Activity Life cycle https://developer.android.com/reference/android/app/Activity.html ,
onCreate() :
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart().
second ,refer fragment life cycle https://www.tutorialspoint.com/android/android_fragments.htm
you are confused as onCreate() is both in activity and fragments right!! let we explore .. onCreateView() :
The onCreate() method in a Fragment is called after the Fragment'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.
Third ,refer adapters ,http://www.edureka.co/blog/what-are-adapters-in-android/
getView() :
1: The LayoutInflater takes your layout XML-files and creates different View-objects from its contents.
2: The adapters are built to reuse Views, when a View is scrolled so that is no longer visible, it can be used for one of the new Views appearing. This reused View is the convertView. If this is null it means that there is no recycled View and we have to create a new one, otherwise we should use it to avoid creating a new.
3: The parent is provided so you can inflate your view into that for proper layout parameters.
All these together can be used to effectively create the view that will appear in your list (or other view that takes an adapter):
public View getView (int position, View convertView, ViewGroup parent){
if( convertView == null ){
//We must create a View:
convertView = inflater.inflate(R.layout.my_list_item, parent, false);
}
//Here we can do changes to the convertView, such as set a text on a TextView
//or an image on an ImageView.
return convertView;
}
You can not use onCreateView() and getView() inside your activity (in case of custom adapter if you wish ,you can create custom adapter inside your activity and in that class you can use getView()).