WHOATEMYNOODLES
WHOATEMYNOODLES

Reputation: 2731

getSupportFragmentManager findFragmentByTag always returning null when trying to find a child fragment

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

Answers (1)

Ben P.
Ben P.

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

Related Questions