0

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
Laurent Meyer
  • 2,766
  • 3
  • 33
  • 57
  • What exactly does not work? Do you get your Runtime Exception? Or is the fragment not removed..? Do you have any information in your logcat? – yennsarah Jun 02 '16 at 11:35
  • @Amy sorry, the fragment is not removed and I have no information/crash in my logcat. Silently fails... – Laurent Meyer Jun 02 '16 at 11:38
  • You don't add your fragmentTransaction to the backstack, so I don't think popping it will bring any result. Have you tried to debug it? Set a breakpoint on the `swipe` fragment and check if it is shown, hidden, twice inflated or something like that. – yennsarah Jun 02 '16 at 12:00
  • @Amy thanks for your help, I found what it was and you couldn't find with only the information I gave, sorry... But the answer is pretty interesting ;) – Laurent Meyer Jun 02 '16 at 12:13

2 Answers2

2

try this one:

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

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

Reference

Community
  • 1
  • 1
Mehta
  • 1,228
  • 1
  • 9
  • 27
1

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!

Laurent Meyer
  • 2,766
  • 3
  • 33
  • 57