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 :

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

  • FragmentActivity creates instance of the fragments

  • Then assigns fragment to container.

  • FragmentManager then displays them.

  • 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.


    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.

    链接地址: http://www.djcxy.com/p/11788.html

    上一篇: iOS中的稀疏图像匹配

    下一篇: 适用于Android的Fragments中容器的用途