Assign container in fragments for Android

I study fragments, but I do not understand the meaning of why fragments require a container.

As I understand it, the work on fragments is as follows:

  • FragmentActivity setContentview refers to an XML file that determines where fragments will be located.

  • FragmentActivity instantiates fragments

  • Then assigns the fragment to the container.

  • Then displays the FragmentManager.

  • The actual fragment class then inflates the layout, and it is this layout that contains all the components of the application user interface.

(please correct me if I miss something here, because now I was just studying).

So, for some reason, we need the purpose of the container, because in all the examples that I saw, this is just an empty XML document in a format document.

Can different fragments use the same container (with its just XML file RelativeLayout)?

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

They have a ListFragment, and when an item is selected using the CallBack interface, we eventually return 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 fragment (left fragment) with the article fragment. Since when it was initialized, we see:

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

Instead ... the ListFragment stays on the left and the right Fragment is updated. But the fragment_container container belongs to firstFragment, it is a ListFragment. And this is not the one that is being updated.

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

+7
source share
1 answer

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

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

Read this and everything will be clear :)

A fragment is part of an Activity and can only exist inside an Activity. Therefore, you need a special type of activity that the fragment can handle - this is FragmentActivity.

FragmentActivity without fragments is almost like normal activity. But he has a FragmentManager for managing (adding, deleting, replacing) fragments. If you want to add a fragment to FragmetnActivity, you must specify where it should be placed (since the fragment does not have to be full-screen, like GooglePlay, a few small fragments). That is why you need a container.

Can different fragments use the same container (with its just XML file RelativeLayout)?

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

+3
source

All Articles