Note that there are some explanatory texts on larger screens.

plurals
  1. POPurpose of Container in Fragments for Android
    text
    copied!<p>I am learning fragments but I am failing to understand the significance behind why fragments requires a Container.</p> <p>The way I understand Fragments work is as follows :</p> <ol> <li><p>FragmentActivity setContentview refers to a xml file which defines where fragments would be located.</p></li> <li><p>FragmentActivity creates instance of the fragments </p></li> <li><p>Then assigns fragment to container. </p></li> <li><p>FragmentManager then displays them. </p></li> <li><p>The actual Fragment class then inflates a layout, and it is this layout which contains all of the applications UI components.</p></li> </ol> <p>(please correct me if I miss something here because I am only learning at the moment).</p> <p>So why is the purpose of the Container why do we even need since in all the examples I have seen it is just a blank relative layout xml document.</p> <p>Can different fragments share the same Container (since its just a RelativeLayout xml file)? </p> <p>So in the example provided by google <a href="http://developer.android.com/training/basics/fragments/creating.html">http://developer.android.com/training/basics/fragments/creating.html</a></p> <p>They have a ListFragment and when item is selected through the use of the CallBack interface we eventually get back to this line of code :</p> <pre><code>// Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction.replace(R.id.fragment_container, newFragment); </code></pre> <p>My other question is:</p> <p>1) Why does this line of code not replace the ListFragment (left side Fragment) with the article fragment. Since when it was initialised we see:</p> <pre><code>getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, firstFragment).commit(); </code></pre> <p>Instead ... the ListFragment remains on the left and the right Fragment is updated. But the container fragment_container belongs to firstFragment this is the ListFragment. And this is not the one that gets updated.</p> <p>Do you see why I have the question ? This is not explained in the tutorial.</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