Search code examples
androidandroid-viewpagerfragmentfragmentmanager

How to handle child fragments in custom calendar with FragmentStatePagerAdapter?


I have a main activity with a linearLayout container called "mainActivityContainer". In this container I make many fragment transactions, managing the backstack for back-navigation. I manage all transactions with getSupportFragmentManager.

Now, I am in fragment sportsFragment which is loaded in the "mainActivityContainer". sportsFragment loads sportsFragmentXml. sportsFragmentXml has a view "mainFragmentContainer" where I load a list of sports. When I click on a date filter from sportsFragmentXml, I add a new fragment with getChildFragmentManager, a customCalendarFragment, into mainFragmentContainer, in order to filter the sport events based on date.

getChildFragmentManager()
.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.add(R.id.mainFragmentContainer, customCalendarFragment)
.addToBackStack(this.getClass().getName())
.commit();

The customCalendarFragment is made of a viewPager which loads, with the help of a FragmentStatePagerAdapter, many smaller fragments (can be tens of smaller fragments, which make-up the calendar). I do this in the getItem(int position) by instantiating dynamically each smaller fragment.

@Override
public Fragment getItem(int position) {
return CalendarAdapterFragmentItem.init(monthsList.get(position), dayFilter);
}

The user can swipe left and right to choose the suitable date. When the user clicks again on the date filter from abcFragment or clicks on a certain date, I remove the customCalendarFragment

FragmentManager childFragmentManager = getChildFragmentManager();
childFragmentManager
.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.remove(customCalendarFragment)
.commit();
childFragmentManager.popBackStack();

Everything good until this point. Then I select a sport from the list contained in the mainFragmentContainer. I open the sport details. I hit back, the onCreateView method from sportsFragment gets called and then the app crashes, getting this error.

java.lang.IllegalArgumentException: No view found for id 0x7f080159 (com.myProject.staging:id/viewPager) for fragment CalendarAdapterFragmentItem{7d97222 #1 id=0x7f080159}

If I do the same, but without opening the calendar, more exactly without making the add/remove transactions for custom calendar, no crash happens.

My question is: why am I getting this error if the customCalendarFragment was added and removed subsequently? The customCalendarFragment contained the viewPager which loaded those many CalendarAdapterFragmentItem smaller fragments. Why is this error reffering to those elements if they were removed? What am I missing?

I studied this post Android Fragment no view found for ID? and this post Communication between nested fragments in Android but I did not find a suitable and working solution for my problem.


Solution

  • After many test and trials, I replaced the getChildFragmentManager with the normal getFragmentManager and this solved my issue.