I'm going to risk my answer:)
I think we all know what Fragment
is. I will start from this principle, if someone who comes to read this answer does not know I recommend reading this question .
The central problem is BackStack
, which is a key element in using Transactions involving Fragments
.
It is known that every transaction that involves addition and removal of Fragments
programmatically in Activity will always see a code like this:
// 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();
BackStack
is a stack, as its name says, which stores screen states against transactions using FragmentManager
in a given FragmentTransaction
. The BackStack
transparently allows the navigation between the Fragments
from stacking the states before each commit
of the transaction.
All transactions are asynchronous, that is, they are staggered to run in the next Loop
of their Main Thread
. If you want to run synchronously, there is the FragmentManager.executePendingTransactions()
method.
If a particular transaction, involving the removal of a Fragment
, is performed and an entry is not added to BackStack
, it will not be possible to restore% using the navigation patterns . According to the Fragment Transaction section.
Below is an explanation with images and actions of the operation of Fragment
.
1. Initial activity state with embedded Fragment1
2.Clickthe(ReplacefromFragment1byFragment2)button
IwouldhaveacodesimilartothefirstoneIposted,todothereplace.Andtheresultwouldbe:
When BackStack
is removed by method Fragment
or remove
(implicitly), its replace
is destroyed (method View
is called) but no change in its state occurs (except changes in onDestroyView
life cycle and that influences the Activity
life cycle), then all Fragment
instance variables are retained.
3. Click on the (Replace from Fragment 2 by Fragment3) button
4.Navigatingbackthroughthe
Inthiscase,wewouldhavetousesuchacodeinFragment
ofthebutton:
getSupportFragmentManager().popBackStack();//ougetFragmentManager().popBackStack();
Theresultbecomes:
In% with% FragmentManager uses the record of the last transaction performed, to do the reverse operation. Removing View.OnClickListener
and adding popBackStack
back. At this point only Fragment3
of Fragment2
is re-created (method View
is called).
5. Navigation back through the Navigation Bar back item
The Fragment2
method of onCreateView
is called. If your onBackPressed
inherits from Activity
or Activity
, calling FragmentActivity
is done by ActionBarActivity
. If there is nothing in popBackStack
it will end the activity normally. In that case do not worry, unless you wanted to do something custom otherwise.
If you're using FragmentActivity
, take a look at the source code of BackStack
. For versions of SDK 11+, the Support Library v4
ja class already has this behavior already included in FragmentActivity.onBackPressed()
The final screen state and Activity
is:
I hope the concept has become clear, or at least helped to begin to understand hehe.
I ended up learning a lot by writing this answer, of course most of the concepts here I removed from the Activity.onBackPressed()
documentation page. And the images made in Paint, I'm not Designer so I beg for patience.