Note that there are some explanatory texts on larger screens.

plurals
  1. POgetChildFragmentManager() on programmatically (dynamically) added Fragments?
    primarykey
    data
    text
    <p>How to use (or "Can we use") <code>getChildFragmentManager()</code> on programmatically (dynamically) added <code>Fragment</code>s?</p> <p><strong>Here is my example.</strong></p> <p>I have one <code>MainActivity</code>, one <code>OuterFrag</code>, and one <code>InnerFrag</code>. I will add the <code>OuterFrag</code> to <code>MainActivity</code> dynamically by the <code>FragmentManager</code>. And also, I will add the <code>InnerFrag</code> to the <code>OuterFrag</code> also dynamically by the <code>FragmentManager</code>. But I want to add <code>InnerFrag</code> exactly as a child of the <code>OuterFrag</code>, not replacing <code>OuterFrag</code> and be the new child of the <code>MainActivity</code>.</p> <p><strong>I want to keep this hierarchy: <code>MainActivity -&gt; OuterFrag -&gt; InnerFrag</code>. So MainActivity can always call OuterFrag.</strong></p> <p>But NOT change from this hierarchy: <code>MainActivity -&gt; OuterFrag</code> to this hierarchy: <code>MainActivity -&gt; InnerFrag</code> that <code>MainActivity</code> will loss the <code>OuterFrag</code>.</p> <p><strong>Here is my example code.</strong></p> <p><strong>MainActivity.java</strong></p> <pre><code>package com.example.frag; import android.os.Bundle; import android.support.v4.app.FragmentActivity; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction().add(R.id.frameLayout, new OuterFrag()).commit(); getSupportFragmentManager().executePendingTransactions(); System.out.println("Before: " + getSupportFragmentManager().findFragmentById(R.id.frameLayout)); ((OuterFrag) getSupportFragmentManager().findFragmentById(R.id.frameLayout)) .addInnerFrag(); System.out.println("After: " + getSupportFragmentManager().findFragmentById(R.id.frameLayout)); } } </code></pre> <p><strong>activity_main.xml</strong></p> <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/frameLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;/FrameLayout&gt; </code></pre> <p><strong>OuterFrag.java</strong></p> <pre><code>package com.example.frag; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class OuterFrag extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.outer_frag, container, false); } public void addInnerFrag() { getFragmentManager().beginTransaction().replace(this.getId(), new InnerFrag()).commit(); getFragmentManager().executePendingTransactions(); // getChildFragmentManager().beginTransaction().add(this.getId(), new InnerFrag()).commit(); // getChildFragmentManager().executePendingTransactions(); } } </code></pre> <p><strong>outer_frag.xml</strong></p> <pre><code>&lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="i am the OUTER frag" /&gt; </code></pre> <p><strong>InnerFrag.java</strong></p> <pre><code>package com.example.frag; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class InnerFrag extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.inner_frag, container, false); } } </code></pre> <p><strong>inner_frag.xml</strong></p> <pre><code>&lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="i am the INNER frag" /&gt; </code></pre> <p>Currently, the above code can run without errors. But it is actually changing <code>InnerFrag</code> as the new child of <code>MainActivity</code>. This can be verified by the two System Print Out statements that the Before: Object and the After: Object is changed. In the <code>OuterFrag.java</code>, if the <code>getChildFragmentManager()</code> statements are run instead of the <code>getFragmentManager()</code> statements, we will get the following runtime error:</p> <pre><code>12-07 02:29:38.406: E/AndroidRuntime(12051): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.frag/com.example.frag.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f070000 (com.example.frag:id/frameLayout) for fragment InnerFrag{46e32748 #0 id=0x7f070000} </code></pre> <p>Using <code>getChildFragmentManager()</code> is theoretically correct. It can be used in a non-programmatically added fragment (which means changing <code>activity_main.xml</code>'s <code>&lt;FrameLayout&gt;</code> to <code>&lt;fragment&gt;</code>, add attribute <code>android:name="com.example.frag.OuterFrag"</code>, and remove the first <code>getSupportFragmentManager()</code> statement in <code>MainActivity.java</code>). And it is keeping the correct hierarchy: <code>MainActivity -&gt; OuterFrag -&gt; InnerFrag</code>. But the words of original fragment (<code>outer_frag.xml</code>) can never be taken away.</p> <p>In conclusion, I want to reference <code>OuterFrag</code> in <code>MainActivity</code> always. And I want <code>OuterFrag</code> act as a placeholder to load different <code>InnerFrag</code>s. In short, I want to call <code>getChildFragmentManager()</code> in <code>OuterFrag</code>, when it is added programmatically (dynamically).</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.
 

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