Reputation: 2731
I have read 10 stackoverflow threads out why my case of getSupportFragmentManager findFragmentByTag is returning null. I've been trying to debug everything for over 2 hours and still can't figure out why.
I have a Child Fragment that is adding another Child Fragment:
getChildFragmentManager().beginTransaction()
.add(R.id.fragmentsubreddit_container, fragmentViewpager, "FRAGMENT_VIEWPAGER")
.commit();
getChildFragmentManager().executePendingTransactions();
activity.findTheFragment();
In my activity's method:
public void findTheFragment(){
FragmentViewpager fragmentViewpager = (FragmentViewpager) getSupportFragmentManager()
.findFragmentByTag("FRAGMENT_VIEWPAGER");
if(fragmentViewpager == null){
Log.d(TAG, "findTheFragment: Returning null");
}
}
What am I doing incorrectly that findFragmentByTag
is returning null?
Upvotes: 1
Views: 541
Reputation: 54204
The FragmentManager
returned by Activity.getSupportFragmentManager()
is different from the one returned by Fragment.getChildFragmentManager()
.
If you want to perform a fragment transaction from the Fragment
level, but be able to "see" it at the Activity
level, then you should use Fragment.getParentFragmentManager()
(or Fragment.getFragmentManager()
on older versions of the AndroidX library).
Upvotes: 2