Note that there are some explanatory texts on larger screens.

plurals
  1. POFragment constantly updated
    text
    copied!<p>I've got a FragmentActivity when I instantiate three different (n, n+1, n+2) Fragments. I need to keep each Fragment updated when user swipes to it, so I used <em>ViewPager.SimpleOnPageChangeListener.onPageSelected</em> in the Fragment Activity, so when user swipes to n+1 or n+2 Fragment and again to n that function update the content. Without using this workaround if I'm in the Fragment n+1, both n and n+2 are already loaded! I'd like instead that the Fragment load when the user swipes to it, without "pre-load".</p> <p>This workaround works fine for me but it has a problem: the n Fragment that is the first in the list at start up of the app doesn't load its content. To load its content I have to swipe to n+1 then go back to n.</p> <p>I know that the content of the Fragment should be setted on the class called at the moment of instantiate the fragment and that extends Fragment class, but in this way I don't know how to keep up to date each Fragment, as I do using <em>onPageSelected</em>.</p> <p>Any suggestions?</p> <p><strong>EDIT 1:</strong> I istantiate my fragments in this way in <em>onCreate()</em>:</p> <pre><code> for(int x = 0; x &lt; 3; x++) { Bundle b = new Bundle(); b.putString( "id" , x ); Fragment myFrag = Fragment.instantiate( myContext , Mm_FragmentPage.class.getName() ); myFrag.setArguments( b ); fragments.add(myFrag); } </code></pre> <p>Then I set the adapter in the <em>ViewPager</em>:</p> <pre><code> mPagerAdapter = new PagerAdapter( super.getSupportFragmentManager() , fragments ); mPager.setAdapter( mPagerAdapter ); </code></pre> <p>Then I use the adapter in the <em>TitlePageIndicator</em></p> <pre><code> titleIndicator = (TitlePageIndicator) findViewById( R.id.titleFragments ); titleIndicator.setViewPager( mPager ); titleIndicator.setOnPageChangeListener( new myPageChangeListener() ); </code></pre> <p>And, at the end, the class <em>PagerAdapter</em>:</p> <pre><code>public class PagerAdapter extends FragmentPagerAdapter { // fragments to instantiate in the viewpager private List&lt;Fragment&gt; fragments; // constructor public PagerAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments) { super(fm); this.fragments = fragments; } // return access to fragment from position, required override @Override public Fragment getItem(int position) { return this.fragments.get(position); } // number of fragments in list, required override @Override public int getCount() { return this.fragments.size(); } @Override public CharSequence getPageTitle(int position) { return getResources().getStringArray( R.array.tab_header_name )[ position ]; } } </code></pre>
 

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