Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of Container in Fragments for Android

I am learning fragments but I am failing to understand the significance behind why fragments requires a Container.

The way I understand Fragments work is as follows :

  1. FragmentActivity setContentview refers to a xml file which defines where fragments would be located.

  2. FragmentActivity creates instance of the fragments

  3. Then assigns fragment to container.

  4. FragmentManager then displays them.

  5. The actual Fragment class then inflates a layout, and it is this layout which contains all of the applications UI components.

(please correct me if I miss something here because I am only learning at the moment).

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.

Can different fragments share the same Container (since its just a RelativeLayout xml file)?

So in the example provided by google http://developer.android.com/training/basics/fragments/creating.html

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 :

// 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);

My other question is:

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:

getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();

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.

Do you see why I have the question ? This is not explained in the tutorial.

like image 493
drlobo Avatar asked Feb 05 '13 18:02

drlobo


1 Answers

Here: http://marakana.com/s/post/1250/android_fragments_tutorial

And here: http://developer.android.com/guide/components/fragments.html

Read this and all will be clear:)

Fragment is a portion of Activity and can exist only inside an Activity. So you need a special type of activity that can handle fragment - it's FragmentActivity.

FragmentActivity without Fragments is almost like a normal Activity. But it has a FragmentManager to manage (add,remove,replace) fragments. When you want to add a Fragment to a FragmetnActivity you should specify where it should be placed (because fragment does not need to be fullscreen, just like GooglePlay-there are multiple small fragments). So this is why you need a container.

Can different fragments share the same Container (since its just a RelativeLayout xml file)?

Yes they can, you can replace one fragment with another within the same container.

like image 62
Michał Z. Avatar answered Oct 23 '22 12:10

Michał Z.