0

I have a ViewPager that has the same fragment UpcomingGamesFragment twelve times which represents the year, hence the twelve fragments, each month has a fragment.

Now I wish to update all the visible fragments in the ViewPager when the user chooses a new platform (PS4, Nintendo switch, etc.) to filter the fragments data with and the update will go through when he closes the Navigation Drawer (The drawer contains a number of platforms (consoles) to choose from).

What I do is the following: I have an update all fragments method in my custom ViewPager adapter which should be able to update all the fragments, but the problem is inside the "update" method which is known by loadReleaseData(int refresh) .It is a UpcomingGamesFragment method. Inside it, I first check if getActivity() isn't null, if it's null this means that the fragment is null (components are null and every else), now the problem is getActivity() is always null, and without the first if statement in the method, the app crashes with a NullPointerException.

Here's my ViewPager adapter:

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> mFragments = new ArrayList<>();
private ArrayList<String> mFragmentsTitle = new ArrayList<>();
private ArrayList<String> mFragmentsFilter = new ArrayList<>();
private Fragment mCurrentFragment;


public void addFragment(Fragment fragment, String title) {
    mFragments.add(fragment);
    mFragmentsTitle.add(title);
}

/**
 * Used for adding UpcomingGamesFragments
 * @param fragment UpcomingGamesFragment
 * @param title UpcomingGamesFragment monthly title [MMMM YYYY]
 * @param filter UpcomingGamesFragment monthly filter to pass into the query
 */
public void addFragment(Fragment fragment, String title, String filter) {
    mFragments.add(fragment);
    mFragmentsTitle.add(title);
    mFragmentsFilter.add(filter);
}

public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    // This page adapter (SectionPagerAdapter) class is used by MainActivity and GamePageActivity
    // If isn't empty pass the query code m_y [we're in MainActivity]
    if (!mFragmentsFilter.isEmpty()) {
        Fragment upcomingFragment = new UpcomingGamesFragment();
        Bundle bundle = new Bundle();
        bundle.putString("fragment_filter", mFragmentsFilter.get(position));
        upcomingFragment.setArguments(bundle);
        return upcomingFragment;
    }
    return mFragments.get(position);
}

public void updateUpcomingGamesFragments() {
    // Really only updates the visible ones
    for (int i = 0; i < mFragments.size(); i++) {
        UpcomingGamesFragment upcomingGamesFragment = (UpcomingGamesFragment) mFragments.get(i);
        upcomingGamesFragment.setFilter(mFragmentsFilter.get(i));
        upcomingGamesFragment.loadReleaseData(1);
    }
}

@Override
public int getCount() {
    return mFragments.size();
}

@Override
public CharSequence getPageTitle(int position) {
    return mFragmentsTitle.get(position);
}

public UpcomingGamesFragment getUpcomingFragment(int position) {
    return (UpcomingGamesFragment) mFragments.get(position);
}

public Fragment getCurrentFragment() {
    return mCurrentFragment;
}

@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    if (getCurrentFragment() != object) {
        // To know we're in Upcoming ViewPager
        // if (!mFragmentsFilter.isEmpty()) {
        mCurrentFragment = (Fragment) object;
        // }
    }
    super.setPrimaryItem(container, position, object);
}

}

Please take note off the updateUpcomingGamesFragments() method. It's the method which gets called when the user closes the drawer. SectionsPagerAdapter is used in my UpcomingViewPagerFragment fragment and so UpcomingViewPagerFragment contains my ViewPager which contains all 12 UpcomingGamesFragment fragment.

Here's my loadReleasesData method which resides in UpcomingGamesFragment

public void loadReleaseData(final int refresh) {
    if (getActivity() == null) {
        Log.d(TAG, "Fragment filter " + mFilter + " [fragment is null]");
        return;
    } else {
        Log.d(TAG, "Updating fragment: " + mFilter);
    }

    if (AppUtil.doesInternetWork(getActivity())) {
        // Viewpager does not load all fragments in memory, contents will be null
        mDatabaseLoading.setVisibility(View.VISIBLE);
        mUpcomingList.setVisibility(View.VISIBLE);
        mEmptyTextInfo.setVisibility(View.GONE);
        noInternetTxt.setVisibility(View.GONE);
        mUpcomingPageLayout.setClickable(false);
        // ... Queries from firebase
    }
}
  • Just override the onAttach method and onActivityCreated method of your UpComingGamesFragment class and see whether these two fragment lifecycle events have occurred before you get to the point of loadReleaseData .. For info, read https://developer.android.com/guide/components/fragments – Rajan Prasad Aug 11 '18 at 03:32
  • @Raymond232 Thank you, but after doing this, will the update method work? –  Aug 11 '18 at 03:35
  • Well you can at least find out if the fragment is getting attached to the activity or not. I am pretty sure it is not. Well, where are you attaching the SectionsPagerAdapter to your activity? – Rajan Prasad Aug 11 '18 at 03:42
  • I'm attaching SectionsPagerAdapter to another fragment and this other fragment is getting attached to my activity and I can safely say that every works perfectly; the fragments are shown with the correct values, but when it comes to updating the fragments everything is null –  Aug 11 '18 at 03:53
  • 2
    Can you just verify for me that your UpcomingGamesFragments are not getting detached inadvertently. Just log the onAttach, onActivityCreated and onDetach methods along with your loadReleaseData method and you will get a better insight into what is happening when you open or close the nav drawer and on other UI events.. – Rajan Prasad Aug 11 '18 at 04:04
  • Ok thank you, I'll report back! –  Aug 11 '18 at 04:08
  • 1
    Use this question for ref: https://stackoverflow.com/questions/6215239/getactivity-returns-null-in-fragment-function – Rajan Prasad Aug 11 '18 at 04:08
  • And I'll also add my viewpager has an offscreen page limit of 1: mViewPager.setOffscreenPageLimit(1); –  Aug 11 '18 at 04:09
  • Which means that the moment two pages go out of screen on any side, one page is being detached and destroyed. It is quite possible that you are getting getActivity() as null for the destroyed fragments. The point is, I think since the number of fragments are fixed in your case, you can do away with this method altogether, or set it as 11 maybe. – Rajan Prasad Aug 11 '18 at 04:20
  • 1
    https://developer.android.com/reference/android/support/v4/view/ViewPager#setoffscreenpagelimit – Rajan Prasad Aug 11 '18 at 04:20

0 Answers0