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
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:
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)
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:
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:
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.
This question is good, I do not know exactly how it works behind the cloths, but follows the principle of stack...
Fragment
s and uses theaddToBackStack
, you are stacking the status prior to change. I hope someone knows how to respond correctly.– Wakim
@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 moreFragments
.– Lucas Santos
Believe that I asked the same question the OS in English and took -6 so far...
– Lucas Santos
Since no one has ventured, I will take a closer look at this concept and try to answer the question
– Wakim
Beauty @Wakim, if you can clarify I believe you will take doubt of many other people too.
– Lucas Santos