4

I'm trying to get a dialogfragment to animate when closed - I've tried to overwrite the dismiss, onDismiss, onCancel and onDestroy methods as below, with no luck - can someone point me in the right direction?

@Override
public void dismiss() {
    if (mDismissAnimationFinished) {
        super.dismiss();
    } else {

        mShrink.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mDismissAnimationFinished = true;
                dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mCurrentContainer.startAnimation(mShrink);
    }
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
Martyn
  • 16,432
  • 24
  • 71
  • 104
  • what is the issue that the those dimiss/cancel functions are not being called at all or that the animation is not being run? Also onAnimationEnd you are recalling dismiss(). Did you recently watch the movie inception ;) – MikeIsrael May 02 '12 at 11:39

2 Answers2

1

I just did this with a regular dialog subclass and my solution should transfer over to DialogFragment exactly.

It looks like you're very close, just that you should be calling super.dismiss() in onAnimationEnd() rather than dismiss(). The reason is that if you call dismiss(), it will recursively call the function you are already in!

EDIT:

Define a wrapper method in the outer class or call it with YourDialogFragment.super().

void parentDismiss() { super.dismiss() }

and call that from your onAnimationEnd().

Even then, you'll still have one more problem with race conditions when you are cancelling the dialog and it is being dismissed by another thread. To solve this, set a boolean mIsDismissed the first time your onDismiss() is called so that if it is called again before another onStart() occurs (you will override onStart() to flip the boolean back) that onDismiss()'s body isn't executed twice.

I have written a solution here, which does not use DialogFragment, just a regular Dialog. It is tested and working, so just look at the logic I use and copy it, or if you want, use it.

https://github.com/tom-dignan/nifty/blob/master/src/com/tomdignan/nifty/dialogs/NiftyProgressDialog.java

Thomas Dignan
  • 7,052
  • 3
  • 40
  • 48
1

You can access the outer class where the listener sits in simply by calling the class with its super. Like this: Classname.super.method()

public void dismiss() {

    yourAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {}

        @Override
        public void onAnimationEnd(Animation animation) {
            classNameOfYourDialog.super.dismiss();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {}
    });
}   
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92