Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A great feature about using <em><code>fragments</code></em> in your <em><code>activity</code></em> is the ability to add, remove, replace, and perform other actions with them, in response to user interaction. Each set of changes that you commit to the activity is called a transaction and you can perform one using APIs in <em><code>FragmentTransaction</code></em>.</p> <blockquote> <p>Each transaction is a set of changes that you want to perform at the same time. You can set up all the changes you want to perform for a given transaction using methods such as add(), remove(), and replace(). Then, to apply the transaction to the activity, you must call commit(). For doing that you have to call *<code>FragmentTransaction</code>.</p> </blockquote> <p><strong>General Use</strong></p> <pre><code>/ 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(); </code></pre> <p>In this example, <em><code>newFragment</code></em> replaces whatever fragment (if any) is currently in the layout container identified by the <code>R.id.fragment_container ID</code>. By calling <em><code>addToBackStack</code></em>(), the <em>replace transaction</em> is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button.</p> <p>If you add multiple changes to the transaction (such as another <code>add</code>() or <code>remove</code>()) and call <code>addToBackStack</code>(), then all changes applied before you call <em>commit</em>() are added to the back stack as a single transaction and the Back button will reverse them all together.</p> <p><strong>The order in which you add changes to a <code>FragmentTransaction</code> doesn't matter, except:</strong> </p> <blockquote> <ol> <li>You must call commit() last</li> <li>If you're adding multiple fragments to the same container, then the order in which you add them determines the order they appear in the view hierarchy</li> </ol> </blockquote> <p><strong>Note:</strong> </p> <p>If you do not call addToBackStack() when you perform a transaction that removes a fragment, then that fragment is destroyed when the transaction is committed and the user cannot navigate back to it. Whereas, if you do call addToBackStack() when removing a fragment, then the fragment is stopped and will be resumed if the user navigates back.</p> <p>Reference </p> <ol> <li><a href="http://developer.android.com/reference/android/app/FragmentTransaction.html" rel="nofollow">http://developer.android.com/reference/android/app/FragmentTransaction.html</a></li> <li><a href="http://developer.android.com/guide/components/fragments.html" rel="nofollow">http://developer.android.com/guide/components/fragments.html</a></li> <li><a href="http://www.vogella.com/articles/AndroidFragments/article.html" rel="nofollow">http://www.vogella.com/articles/AndroidFragments/article.html</a></li> <li><a href="http://www.javacodegeeks.com/2013/06/android-fragment-transaction-fragmentmanager-and-backstack.html" rel="nofollow">http://www.javacodegeeks.com/2013/06/android-fragment-transaction-fragmentmanager-and-backstack.html</a></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload