Note that there are some explanatory texts on larger screens.

plurals
  1. POGet the current fragment object
    text
    copied!<p>In my <code>main.xml</code> I have</p> <pre><code> &lt;FrameLayout android:id="@+id/frameTitle" android:padding="5dp" android:layout_height="wrap_content" android:layout_width="fill_parent" android:background="@drawable/title_bg"&gt; &lt;fragment android:name="com.fragment.TitleFragment" android:id="@+id/fragmentTag" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;/FrameLayout&gt; </code></pre> <p>And I'm setting fragment object like this </p> <pre><code>FragmentManager fragmentManager = activity.getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Fragment newFragment = new FragmentType1(); fragmentTransaction.replace(R.id.frameTitle, casinodetailFragment, "fragmentTag"); // fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); </code></pre> <p>It is setting different types of Fragment objects (<code>FragmentType2,FragmentType3,...</code>) at different time. Now at some point of time I need to identify which object is currently there.</p> <p>In <strong>short</strong> I need to do something like this:</p> <pre><code>Fragment currentFragment = //what is the way to get current fragment object in FrameLayout R.id.frameTitle </code></pre> <p>I tried the following </p> <pre><code>TitleFragment titleFragmentById = (TitleFragment) fragmentManager.findFragmentById(R.id.frameTitle); </code></pre> <p>and </p> <pre><code> TitleFragment titleFragmentByTag = (TitleFragment) fragmentManager.findFragmentByTag("fragmentTag"); </code></pre> <p>But <strong>both the objects (titleFragmentById and titleFragmentByTag ) are <code>null</code></strong><br> Did I miss something?<br> I'm using <code>Compatibility Package, r3</code> and developing for <code>API level 7</code>.</p> <p><code>findFragmentById()</code> and <code>findFragmentByTag()</code> will work if we have set fragment using <code>fragmentTransaction.replace</code> or <code>fragmentTransaction.add</code>, but will <code>return null</code> if we have set the object at xml (like what I have done in my <code>main.xml</code>). I think I'm missing something in my XML files.</p>
 

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