Laurent Meyer
Laurent Meyer

Reputation: 2881

Remove of the ChildFragmentManager is not working as expected

I have a Parent Fragment containing a Child Fragment which displays some data.

At some point the Child Fragment broadcasts that the user is finished (that there is no data to be displayed). At this point I want to remove this useless fragment but I somehow don't success. Am I missing something?

The way I add the fragment to the container (and it works really good):

if (swipeFragment == null)
    swipeFragment = new SwipeViewFragment();
if (getActivity() != null) {
    getChildFragmentManager().beginTransaction().add(R.id.jobSearchContainer, swipeFragment, "swipe").commitAllowingStateLoss();
    status = SWIPE;
}

The way I planed to remove it (but it doesn't work and it is all the variations I tried):

Fragment swipe = getChildFragmentManager().findFragmentByTag("swipe");
if (swipe == null){
    throw new RuntimeException("Nope");
}
getChildFragmentManager().beginTransaction().remove(swipe).commit();
getChildFragmentManager().beginTransaction().hide(swipe).commit();
getChildFragmentManager().popBackStack();
getFragmentManager().beginTransaction().remove(swipe).commit();

I am missing something?

Thanks

PS: When I say that it doesn't work: I mean the fragment is not getting removed and I have no output in logcat

UPDATE

    Fragment swipe = getChildFragmentManager().findFragmentByTag("swipe");
    if (swipe == null){
        throw new RuntimeException("Nope");
    }
    Log.d("DEBUG", ""+getChildFragmentManager().getFragments().size());
    getChildFragmentManager().beginTransaction().remove(swipe).commit();
    getChildFragmentManager().popBackStack();
    Log.d("DEBUG", ""+getChildFragmentManager().getFragments().size());

Has for a result:

1
1

Upvotes: 0

Views: 4605

Answers (2)

Laurent Meyer
Laurent Meyer

Reputation: 2881

I debugged it and I finally found what it was all about and I'm sorry it is not related to the code I posted. Nevertheless, it is very interesting!

The problem was in the onCreateView of the view:

It used to be like this:

View v = inflater.inflate(R.layout.swipe_layout, container);
// Bla bla
return null;

THIS IS A PROBLEM because I think the fragment is then somehow not associated with the view... But it works perfectly because the view uses the container and everything is fine

Instead, use:

View v = inflater.inflate(R.layout.swipe_layout, container, false);
// Bla bla
return v;

If you do so, everything is fine!

I detected the problem because I tried to make the view transparent and I called the Fragment.getView() which returned me null.

That was a nasty one. Thx for your help guys!

Upvotes: 1

Mehta
Mehta

Reputation: 1226

try this one:

Fragment swipe = getChildFragmentManager().findFragmentByTag("swipe");
if (swipe == null){
    throw new RuntimeException("Nope");
}

getChildFragmentManager().beginTransaction().remove(swipe).commit();
getChildFragmentManager().popBackStack();

Reference

Upvotes: 2

Related Questions