What does popBackStack()?

Asked

Viewed 1,755 times

4

I can’t understand or find anywhere that makes me understand what the method does getActivity().getSupportFragmentManager().popBackStack();

Not even reading the documentation I could understand, someone could explain me well explained and if possible by making analogies, I understand better with analogies.

  • This question is good, I do not know exactly how it works behind the cloths, but follows the principle of stack... Fragments and uses the addToBackStack, you are stacking the status prior to change. I hope someone knows how to respond correctly.

  • @Walkim you still use, I don’t even get to use because I can’t understand. Rsrs. Let’s wait for the answer and see what will happen. I need to understand because I hardly use it very much Activitys for navigation between screens, use more Fragments.

  • Believe that I asked the same question the OS in English and took -6 so far...

  • Since no one has ventured, I will take a closer look at this concept and try to answer the question

  • Beauty @Wakim, if you can clarify I believe you will take doubt of many other people too.

1 answer

10


I’ll risk my answer :)

I believe we all know what it is Fragment. I am going to start from this principle, if anyone reading this answer does not know I recommend reading this question.

The central problem is BackStack, which is a key item in the use of Transactions involving Fragments.

It is known that every transaction involving addition and removal of Fragments programmatically in Activity you will always see a code this way:

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

The BackStack is a stack, as its name says, that stores screen states in relation to transactions using the FragmentManager in a certain FragmentTransaction. The BackStack allows, in a transparent way, navigation between the Fragments states stacking before each commit of the transaction.

All transactions are asynchronous, that is, they are staggered to run in the next Loop of his Main Thread. If you want to run synchronously, there is the method FragmentManager.executePendingTransactions().

If a particular transaction, which involves the removal of a Fragment, is performed and no entry is added in the BackStack, it will not be possible to restore the Fragment removed using the navigation standards. According to the section of Fragment Transaction.

Below is an explanation of how the BackStack.


1. Initial status of Activity with embedded Fragment1

inserir a descrição da imagem aqui

2. Click the button (Replace Fragment1 by Fragment2)

It would have a code similar to the first one I posted, to do replace. And the result would be:

inserir a descrição da imagem aqui

When a Fragment is removed by the method remove or replace (implicitly), his View is destroyed (the method onDestroyView is called) but there is no change in its state (except changes in the life cycle of the Activity and which influences the life cycle of Fragment), then all the instance variables of Fragment are maintained.

3. Click the button (Replace Fragment 2 by Fragment3)

inserir a descrição da imagem aqui

4. Navigation back through button

In that case, we would have to use such a code on View.OnClickListener button:

getSupportFragmentManager().popBackStack();
// ou
getFragmentManager().popBackStack();

The result becomes:

inserir a descrição da imagem aqui

In the popBackStack Fragmentmanager uses the record of the last transaction performed to do the reverse operation. Removing the Fragment3 and adding back the Fragment2. At that moment only the View of Fragment2 is recreated (method onCreateView is called).

5. Navigation back by Navigation Bar back item

The method onBackPressed of Activity is called. If your Activity inherit from FragmentActivity or of ActionBarActivity, the call the popBackStack is made by FragmentActivity. If there’s nothing in the BackStack it will finish the Activity normally. In that case do not worry, unless you wanted to do something custom otherwise.

If using the Support Library v4, a look at the source code of FragmentActivity.onBackPressed(). For 11+ SDK versions, the class Activity already has this behavior is already included in Activity.onBackPressed()

The final state of the screen and the BackStack stays:

inserir a descrição da imagem aqui

I hope the concept has been clear, or at least helped to begin to understand hehe.

I ended up learning a lot when writing this answer, of course most of the concepts here I removed from the documentation page on Fragments. And the images made in Paint, I’m not Designer so I ask patience.

  • first thank you for responding and trying to help the community. You said, "In the popBackStack the Fragmentmanager uses the record of the last transaction performed, to do the reverse operation. Removing Fragment3 and adding back Fragment2." In the case then if I give one popBackStack(), when you return to Fragment, it will come back exactly as it was before the transaction or it will be reloaded?

  • Let me see if I understand item 5. I have a class that extends FragmentActivity or ActionBarActivity, then when I press the back button in any my Fragment that is on the screen, the method onBackPressed() of my class will call the super of Activity which in turn will call popBackStack()?

  • 1

    In relation to the second comment, that’s almost it. In the support lib who calls the popBackStack are the two subclasses. In the SDK 11+, it is the Activity itself that calls. I even put a link pro source code of the classes. I will answer the first comment.

  • Okay @Wakim. No problem, I’m on hold.

  • @Lucassantos, I did some tests and my assumption was wrong... Soon I deleted the comment as it was wrong. What happens when giving remove in a Fragment is: Your View is destroyed and there is no state rescue because the Fragment is not destroyed when the popBackStack, that instance of Fragment is reused, so the instance variables are "intact" (taking away memory problems, which escapes the scope of the question). I’ll include that in my reply.

  • OK @Wakim. I get it.

  • @Wakim, beautiful "risky". Enlightening, I found nothing very good about Fragment’s Backstack or in English, only in the official documentation that is not so enlightening, +1

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.