Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Where to put activity's onCreate() code in a fragment?

I'm converting all my Activities to Fragments so that I can use them in a ViewPager.

I've searched for this but I couldn't find a satisfying answer, so that's why I'm asking it here.

In my Activities, I've written some code in the onCreate() method. I for example call some findViewById()s in order to link some xml-buttons to my Activity. I also make some views invisible in the onCreate(), set an OnClickListener(), fill a TextView with text and remove a Notification, all in the onCreate() method.

My question is: Where should I put this code in the fragment? In the onCreate()? onCreateView()? onActivityCreated()? and why?

Many thanks in advance!

like image 594
Xander Avatar asked Mar 07 '13 17:03

Xander


People also ask

Does fragment have onCreate?

onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.

How can set activity to fragment?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

What is the purpose of super onCreate () in android?

Q 9 – What is the purpose of super. onCreate() in android? The super. onCreate() will create the graphical window for subclasses and place at onCreate() method.

What are the roles of onCreate () and onCreateView () callback methods in the fragments life cycle?

onCreate() : Initialize essential components and variables of the Fragment in this callback. The system calls this method when the Fragment is created. Anything initialized in onCreate() is preserved if the Fragment is paused and resumed. onCreateView() : Inflate the XML layout for the Fragment in this callback.


2 Answers

Although Pragnani's answer is close, there's little educational value in it. Besides, there's a more appropriate option to his 2nd statement.

Where should I put this code in the fragment? In the onCreate()? onCreateView()? onActivityCreated()? and why?

The short answer is: either onCreateView() or onActivityCreated() will do. The view hierarchy won't be created until onCreateView(), so that's the earliest point in the fragment's life cycle that you could inflate the views and attach click listeners etc. Since onActivityCreated() will always be run after onCreateView(), that's a suitable location too. onCreate() may be skipped in favour of the system temporarily detaching the fragment and reattaching it, e.g. when retaining fragments.

Pragnani is correct by pointing out that inflating the views of a fragment is slightly different from inflating views in an activity. More specifically: a fragment does not define a findViewById() method, so you'll need to call it on some other object.

Rather than using getActivity().findViewById(), you'll want getView().findViewById(). The reason for this is that if you use the activity for the view lookups, then you'll get into trouble when multiple fragments with the same view IDs are attached to it. This will be the case if you reuse view ids in the layouts of your various fragments, or if you show two identical fragments that display different data. In both cases, only the first match would ever be returned, whereas you really want to the view to be looked up in the conext of the fragment. That's exactly what getView() returns, the fragment's root view (that you returned in onCreateView()), and thus limits the scope of the lookup appropriately.

like image 178
MH. Avatar answered Nov 08 '22 05:11

MH.


1.Left the onCreate empty and just call super.onCreate()

2.Instead of findViewById() use getActivity().findViewById() always use getActivity() where you need context of the view.

 Do all other operations in onCreateview()
like image 43
Pragnani Avatar answered Nov 08 '22 06:11

Pragnani