0

I have a couple of Fragments with RecyclerViews in a ViewPager. On actions in the first Fragment, the remaining Fragments change what they display.

I can access all the Fragments from the first one and call update methods on them. The problem is, the updates do not happen if I do not swipe to the other Fragments. In fact, before tapping any action buttons in the first Fragment, I have to swipe to the other Fragments (without purpose) just to keep the app from crashing with a NullPointerException.

The reason I guess is because the remaining Fragments are null as they are not brought into view.

I have tried this approach so far, in vain:

public void updatePager(int pagePosition)
{
    ((NMMain) getActivity()).dsFragment.updateDSFragment(dView);

    nMParentViewPager = (ViewPager) getActivity().findViewById(R.id.gsSupremePager);
    nMParentViewPager.getAdapter().notifyDataSetChanged();
    nMParentViewPager.setCurrentItem(pagePosition, true);
} // of updatePager()

Log

After clicking action buttons in Fragment1 after swiping to fragments:

02-06 14:53:21.563 7765-8678/com.my A/NMDBHelper class: Create View-1 query: CREATE VIEW View_Temp 02-06 14:53:21.585 7765-8678/com.my A/NMDBHelper class: Create dealsView-2 query: CREATE TABLE D_View 02-06 14:53:21.748 7765-7765/com.my E/DSFragment: In frag2.updateFragment(): dView.size() = 3

After clicking action buttons in Fragment1 without swiping to any fragments:

02-06 14:58:52.691 11967-11967/com.nearme E/AndroidRuntime: FATAL EXCEPTION: main Process: com.my, PID: 11967 java.lang.NullPointerException at com.my.s.Frag2.updatePager(Frag2.java:xxx)

So my question,

  • Irrespective of the visibility of a Fragment in a ViewPager, how can they (Fragments) be kept ready with the latest content?
Narayana J
  • 307
  • 5
  • 17
  • Doesn't `FragmentStatePagerAdapter` manage Fragment references okay? – OneCricketeer Feb 06 '16 at 08:29
  • @cricket_007, my pager adapter extends `FragmentStatePagerAdapter`,Is there something 'else' that must be done in the adapter, to meet my requirement? – Narayana J Feb 06 '16 at 08:59
  • Maybe. Hard to tell in the code you are showing, but somewhere you are losing the references to the "pages" in the ViewPager, which causes them to be null. I have a project that I used an ArrayList of Fragments for the Adapter (kinda like a custom ArrayAdapter for Fragments) that seemed to work out okay – OneCricketeer Feb 06 '16 at 09:01
  • I do not use any collection for fragments. Surely, the fragments cannot be null, because if I swipe to them (pointlessly), come back to Fragment 1 and do an action which runs a method in the fragment I just swiped to, there's no `NPE`. If I don't swipe to that fragment and do the action, there is an `NPE` that crashes the app. – Narayana J Feb 06 '16 at 09:15
  • Hmm, maybe you should add the logcat and relevant code to your question instead of me just guessing what the solution should be – OneCricketeer Feb 06 '16 at 09:17
  • Please use the edit link under your question to add that information rather than using the comments – OneCricketeer Feb 06 '16 at 10:12
  • Sorry about the messy comment. Please see the updated question. – Narayana J Feb 06 '16 at 17:23

1 Answers1

0

The way I was thinking and the way I worded the question - oh so complex.

All I was looking to do was pre-load fragment pages. This post resolved it. So, ViewPager.setOffscreenPageLimit() is the answer.

Though this is not my answer, hope it will help someone who bumps into similar complexity and lands here.

Thanks for looking in, @cricket_007 and all the others.

Community
  • 1
  • 1
Narayana J
  • 307
  • 5
  • 17