Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you call <a href="http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#replace%28int,%20android.support.v4.app.Fragment,%20java.lang.String%29" rel="noreferrer">FragmentTransaction.replace(...)</a>, Android will effectively perform a sequence of <a href="http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#remove%28android.support.v4.app.Fragment%29" rel="noreferrer">FragmentTransaction.remove(...)</a> (for all Fragments currently added to that container) and <a href="http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#add%28int,%20android.support.v4.app.Fragment,%20java.lang.String%29" rel="noreferrer">FragmentTransaction.add(...)</a> (for your supplied Fragment). Removing a Fragment from the <code>FragmentManager</code> will cause the Fragment to be destroyed and its state will no longer be managed. Most noticeably, when you re-add the Fragment all of the views will have been reset. Note: since you are reusing the same Fragment instance, the Fragment will still keep the value any instance variables.</p> <p>One solution to this problem would be to use <a href="http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#detach%28android.support.v4.app.Fragment%29" rel="noreferrer">FragmentTransaction.detach(Fragment)</a> and <a href="http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#attach%28android.support.v4.app.Fragment%29" rel="noreferrer">FragmentTransaction.attach(Fragment)</a> when switching. This will cause the Fragment views to be recreated (<code>onDestroyView()</code> &amp; <code>onCreateView()</code> will be called), but the instance state bundle will be saved and given back to you between calls and so the view state can be maintained. This is the approach taken by <a href="http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html" rel="noreferrer">FragmentPagerAdapter</a> when it tries to switch between Fragments.</p> <p>Alternatively, you could allow the Fragments to be destroyed, but maintain their saved state for them independently. This would use less memory, at the expense of a slower switching time. Methods of note would be <a href="http://developer.android.com/reference/android/app/FragmentManager.html#saveFragmentInstanceState%28android.app.Fragment%29" rel="noreferrer">FragmentManager.saveFragmentInstanceState(Fragment)</a> and <a href="http://developer.android.com/reference/android/app/Fragment.html#setInitialSavedState%28android.app.Fragment.SavedState%29" rel="noreferrer">FragmentManager.setInitialSavedState(Fragment.SavedState)</a>, in conjuction with adding/removing. This is the approach taken by <a href="http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html" rel="noreferrer">FragmentStatePagerAdapter</a>.</p> <p>You can have a look at the <a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/android/support/v4/app/FragmentPagerAdapter.java#FragmentPagerAdapter" rel="noreferrer">source for FragmentPagerAdapter</a> and the <a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/android/support/v4/app/FragmentStatePagerAdapter.java#FragmentStatePagerAdapter" rel="noreferrer">source for FragmentStatePagerAdapter</a> for implementation hints.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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