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
}
}