3

So I have a case where the app crashes if I call popBackStack while the transition animation is running. Is there any clean way of stopping any ongoing transition, or at least check if anyone is running?

The exception I'm getting:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:3577)

A couple of facts which may or may not be relevant to why this exception is occuring:

  • I'm using standard Fragment-implementation, not supportFragment (reason being I wanted to use SettingsFragment).
  • I'm using setRetainInstance(true) on the Fragments
  • I did try to remove the child view from the parent in the Fragment's onCreateView, but this fails for some reason (childView.getParent() still not null after calling parentView.remove(childView))

Solutions I've considered:

  1. Set retainInstance=false
  2. Wait until the animation ends before executing popBackStack
  3. Finding a way to cleanly abort / reverse the transition

no.1 is not really an option since it will break the UX for the user and also cause a lot of refactoring.

no.2 is the one I'm considering most now but I don't like how I have to move code related to animation initialization and implement a bunch of code for this workaround

no.3 Is the solutioin I'd go for if I knew how. Anyone?

Community
  • 1
  • 1
Nilzor
  • 18,082
  • 22
  • 100
  • 167

1 Answers1

0

You need to detach your cached view from its parent:

@Override
    public void onDestroyView() {
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }
        super.onDestroyView();
    }
bart
  • 304
  • 1
  • 3
  • 13