Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I want to offer an alternate solution for perhaps a slightly different case, since many of my searches for answers kept leading me to this thread.</p> <p><strong>My case</strong> - I'm creating/adding pages dynamically and sliding them into a ViewPager, but when rotated (onConfigurationChange) I end up with a new page because of course OnCreate is called again. But I want to keep reference to all the pages that were created prior to the rotation.</p> <p><strong>Problem</strong> - I don't have unique identifiers for each fragment I create, so the only way to reference was to somehow store references in an Array to be restored after the rotation/configuration change.</p> <p><strong>Workaround</strong> - The key concept was to have the Activity (which displays the Fragments) also manage the array of references to existing Fragments, since this activity can utilize Bundles in onSaveInstanceState</p> <pre><code>public class MainActivity extends FragmentActivity </code></pre> <p>So within this Activity, I declare a private member to track the open pages</p> <pre><code>private List&lt;Fragment&gt; retainedPages = new ArrayList&lt;Fragment&gt;(); </code></pre> <p>This is updated everytime onSaveInstanceState is called and restored in onCreate</p> <pre><code>@Override protected void onSaveInstanceState(Bundle outState) { retainedPages = _adapter.exportList(); outState.putSerializable("retainedPages", (Serializable) retainedPages); super.onSaveInstanceState(outState); } </code></pre> <p>...so once it's stored, it can be retrieved...</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState != null) { retainedPages = (List&lt;Fragment&gt;) savedInstanceState.getSerializable("retainedPages"); } _mViewPager = (CustomViewPager) findViewById(R.id.viewPager); _adapter = new ViewPagerAdapter(getApplicationContext(), getSupportFragmentManager()); if (retainedPages.size() &gt; 0) { _adapter.importList(retainedPages); } _mViewPager.setAdapter(_adapter); _mViewPager.setCurrentItem(_adapter.getCount()-1); } </code></pre> <p>These were the necessary changes to the main activity, and so I needed the members and methods within my FragmentPagerAdapter for this to work, so within</p> <pre><code>public class ViewPagerAdapter extends FragmentPagerAdapter </code></pre> <p>an identical construct (as shown above in MainActivity )</p> <pre><code>private List&lt;Fragment&gt; _pages = new ArrayList&lt;Fragment&gt;(); </code></pre> <p>and this syncing (as used above in onSaveInstanceState) is supported specifically by the methods</p> <pre><code>public List&lt;Fragment&gt; exportList() { return _pages; } public void importList(List&lt;Fragment&gt; savedPages) { _pages = savedPages; } </code></pre> <p>And then finally, in the fragment class </p> <pre><code>public class CustomFragment extends Fragment </code></pre> <p>in order for all this to work, there were two changes, first</p> <pre><code>public class CustomFragment extends Fragment implements Serializable </code></pre> <p>and then adding this to onCreate so Fragments aren't destroyed</p> <pre><code>setRetainInstance(true); </code></pre> <p>I'm still in the process of wrapping my head around Fragments and Android life cycle, so caveat here is there may be redundancies/inefficiencies in this method. But it works for me and I hope might be helpful for others with cases similar to mine.</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. 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