Note that there are some explanatory texts on larger screens.

plurals
  1. POFragmentPagerAdapter with ViewPager and two Fragments. Go to the first from the second and update first's text
    primarykey
    data
    text
    <p>I'm not familiar with <code>FragmentPagerAdapter</code>, so this is going to be one of those questions that we (you) read the description critically.</p> <p><strong>Structure:</strong> I have a <code>FragmentPagerAdapter</code> (code below), that will hold two fragments at a time. The first displays book excerpts, and the second a list of book titles.</p> <p><strong>Goal:</strong> I want to achieve what is described in the title: the user can navigate to the second fragment in the pager, click on a title, and then I want to move the user back to the first fragment and tell the first fragment to update the text. The first fragment has a <code>triggerRefresh</code> method for that.</p> <p><strong>Code:</strong> I believe my problem happens because of the way <code>FragmentPagerAdapter</code> reuses/creates the Fragments (which I don't understand). This is my class:</p> <pre><code>static class MyFragmentPagerAdapter extends FragmentPagerAdapter { public MyFragmentPagerAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return NUM_ITEMS; } @Override public Fragment getItem(int position) { switch(position) { case 0: return new ExcerptsFragment(); case 1: return new BookListFragment(); default: throw new IllegalArgumentException("not this many fragments: " + position); } } } </code></pre> <p>This is how I created the relevant members:</p> <pre><code>ViewPager mViewPager = (ViewPager) findViewById(R.id.pager); MyFragmentPagerAdapter mFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager()); mViewPager.setAdapter(mFragmentPagerAdapter); </code></pre> <p>And this is what I've tried elsewhere in my Activity, when I receive the callback from the book titles Fragment with the title selected:</p> <pre><code>mViewPager.setCurrentItem(0); // back to excerpts screen page. It's OK. // Here's the problem! How to identify the fragment 0 // to ExcerptsFragment and call its triggerRefresh()?!? </code></pre> <p><strong>Series of problems:</strong></p> <p>Calling the adapter's <code>getView()</code> won't work because it will return a new instance of <code>ExcerptsFragment</code>, which is not the one currently attached (as expected, throws the exception).</p> <p>I've seen many people here (<a href="https://stackoverflow.com/a/9214669/489607">example</a>) just storing fragments in the <code>getView()</code>. Is that right? Because by looking at the official examples, seems like an anti-pattern to me (defeat the automatic reference by holding the items). And that is also the opinion <a href="https://stackoverflow.com/a/7098081/489607">here</a> and <a href="https://stackoverflow.com/a/9759024/489607">here</a> (and looks right to me).</p> <p>Any suggestions? I wouldn't be surprised if I'm not understanding all of this one bit...</p>
    singulars
    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.
 

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