-6

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?

Deepa MG
  • 188
  • 1
  • 15
  • There is no `getView()` method in Activity. – Sergey Glotov Nov 30 '16 at 05:19
  • Please thoroughly refer the activity lifecycle in Android. And also kindly use the https://developer.android.com/reference/ for all your basic doubts. Each and every method has been explained clearly. Still if u have doubts StackOverflow is always to your rescue. – UserName_Untold Nov 30 '16 at 05:21
  • i have seen using getView() in fragment.if i need to use graphical activities n Activity() class then how can i do it? – Deepa MG Nov 30 '16 at 05:22
  • onCreate() method is generally used in activity and onCreateview() method is used in fragment and you can use getView() in fragment for any component initialization like button,etc. – Vishal Senjaliya Nov 30 '16 at 05:22
  • There is a good explantion here .http://stackoverflow.com/a/32656807/3111083 – Sunil Sunny Nov 30 '16 at 05:25

2 Answers2

3

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)

AndiGeeky
  • 11,266
  • 6
  • 50
  • 66
  • @AndiGeeky...Refering to your answer ,I cant use **OnCreateView()** and **getView()** in my **Activity()**.Right?.If i need to use these then i need to create `Fragment class`! – Deepa MG Nov 30 '16 at 05:37
  • @ Deepa MG: You can use OncreateView() in `Activity` that depends what is your use-case and for `getView()` you only can use in `Fragment`!! – AndiGeeky Nov 30 '16 at 05:39
  • Oh !! okey i got it.And it made my work easy now :-)Thank you all for reply:-) – Deepa MG Nov 30 '16 at 05:41
  • @Deepa MG: Anytime!! – AndiGeeky Nov 30 '16 at 05:42
  • can i use same layout for OnCreate() Ex: `@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_launch);` and OnCreateView() Ex: `public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.activity_launch, container, false);` – Deepa MG Nov 30 '16 at 06:03
  • @DeepaMG: Yes you can but read carefully documents that `onCreateView()` for `Activity` is not a life cycle method. So It will not be called automatically!! You need to call it manually.!! – AndiGeeky Nov 30 '16 at 06:21
1

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()).

JAAD
  • 12,349
  • 7
  • 36
  • 57
Radhey
  • 2,139
  • 2
  • 24
  • 42