There is a FragmentManager, it manages two structures: such as the transaction stack and the list of fragments.

And I'm trying to understand what these structures are?

Here is an example code

Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if(fragment == null){ fragment = new CrimeFragment(); fm.beginTransaction() // Создать новую транзакцию фрагментов .add(R.id.fragmentContainer,fragment) // добавляем фрагмент в списрк .commit(); // закрепляем } 

As a result, this code does the following:

Getting an existing fragment by the container view ID. (From the beginning, we will send a fragment with the container identifier from FragmentManager. If this fragment is already in the list, FragmentManager returns it. // What is this fragging ??

Question: what if there are several of these fragments? How will he return a few?

So, if fragment == null, then we create a fragment, create a new transaction, and add the fragment to the list of fragments, then close it.

What then is a transaction stack?

  • The book "Android. Programming for professionals" read? In the same place everything seems to be explained. You refer to the fragment by its container id. You can replace the contents of the container with another fragment. If you have several fragments simultaneously in activit, then each of them should have its own container with its own id. Transaction stack is used to quickly switch between different fragments - Fitz
  • @Fitz, I would like to hear about the transaction stack in more detail - Martinez Toni 6:09 pm
  • @Fitz, I have 3 fragments, first I displayed 1 fragment, then 2 fragment (further activity broke), there is onCreate activity and restoration of the fragment, but as I understand it, only the fragment that was displayed last is restored? Fragment 2 does not recover with its data? - Martinez Toni
  • Well, for example, replace one fragment with another in the activation and add it to the stack with the command addToBackStack ("name"). If you then click Back, then the previous fragment will be returned first, instead of quitting the activation. If the transaction stack is empty, then after pressing Back, the activation closes - Fitz
  • what does "activity broke" mean? if you want the fragment to worry about screen rotation, then you can enable it setRetainInstance (true) - Fitz

1 answer 1

What is this framgent ??

With the help of the findFragmentById(int id) method you request a fragment with a specific identifier of the container view ( int id ), that's what you get.

In the .add(R.id.fragmentContainer,fragment) line .add(R.id.fragmentContainer,fragment) this same identifier is R.id.fragmentContainer .

Question: what if there are several of these fragments? How will he return a few?

They can not be several. At any time, a single container view identifier can:

  1. Match no fragment, then findFragmentById(int id) return null .
  2. Match one slice.

The transaction stack is, relatively speaking, some list that stores (if you tell him) the actions that you perform with your fragments.

  • And what is the list of fragments? Where is he? I can not understand something. Here is a fragment Manager) And where is the list of fragments ?? I have 3 fragments, at first I displayed 1 fragment, then 2 fragment (further activity broke), there is onCreate activity and restoration of the fragment, but I understand that only the fragment that was displayed last is restored? Fragment 2 does not recover from its data (if I want to display it?) - Martinez Toni
  • @MartinezToni, List of fragments You can get FragmentManager 'a with the getFragments() method. For other questions, there are thoughts, but it is better to wait for more competent comrades. - s8am
  • How to add pharmagment to the list of fragments? - Martinez Toni
  • @MartinezToni fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit(); - s8am
  • Ie I create a transaction that adds the fragment to the list of fragments correctly?) And I replace the fragments from the list of fragments right? - Martinez Toni