Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Although Pragnani's answer is close, there's little educational value in it. Besides, there's a more appropriate option to his 2nd statement.</p> <blockquote> <p>Where should I put this code in the fragment? In the onCreate()? onCreateView()? onActivityCreated()? and why?</p> </blockquote> <p>The short answer is: either <code>onCreateView()</code> or <code>onActivityCreated()</code> will do. The view hierarchy won't be created until <code>onCreateView()</code>, so that's the earliest point in the fragment's life cycle that you could inflate the views and attach click listeners etc. <a href="http://developer.android.com/guide/components/fragments.html#Lifecycle" rel="noreferrer">Since <code>onActivityCreated()</code> will always be run after <code>onCreateView()</code></a>, that's a suitable location too. <code>onCreate()</code> may be skipped in favour of the system temporarily detaching the fragment and reattaching it, e.g. when <a href="http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance%28boolean%29" rel="noreferrer">retaining fragments</a>.</p> <p>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 <code>findViewById()</code> method, so you'll need to call it on some other object.</p> <p>Rather than using <code>getActivity().findViewById()</code>, you'll want <strong><code>getView().findViewById()</code></strong>. 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 <a href="http://developer.android.com/reference/android/app/Fragment.html#getView%28%29" rel="noreferrer"><code>getView()</code></a> returns, the fragment's root view (that you returned in <code>onCreateView()</code>), and thus limits the scope of the lookup appropriately.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload