Q:

Disable clicks when fragment adding animation playing

I have a method, which perform a fragment adding animation and a new fragment fills all screen:
public void addFragmentWithAnimation(Fragment fragment, boolean addToBackStack){
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right, R.anim.slide_in_left, R.anim.slide_out_right);
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.add(R.id.lay_fragments, fragment);
    commitTransaction(transaction);
}

But when animation playing user still allowed to click on buttons on the view below and make another ui actions, which could cause to unpredictable results. Is there an any way to block all user actions in app, until the end of animation?

A:

A modification of Beloo's answer for non-support Fragments keeps the code encapsulated a bit better. Just use the start and stop of the listener to set and clear the flag...
@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {

        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            }

        });
    }
    return animator;
}

A:

As @yedidyak suggest i added listener to fragment animation as follows:
In base parent fragment:
 @Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    Animation animation;
    if (nextAnim == 0 ){
        animation = super.onCreateAnimation(transit, enter, nextAnim);
    } else {
        animation = AnimationUtils.loadAnimation(getActivity(), nextAnim);
        animation.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {}
            public void onAnimationRepeat(Animation animation) {}

            public void onAnimationEnd(Animation animation) {
                getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
                // additional functionality
            }
        });
    }
    return animation;
}

In activity add fragment method i added code to block user touch events:
public void addFragmentWithAnimation(Fragment fragment, boolean addToBackStack, @MagicConstant FragmentAnimation animation){
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    FragmentTransaction transaction = ...
}

FLAG_NOT_TOUCHABLE was added in fragment transaction part of code because user can dispatch some events between it and onAnimationStart call.
If somebody knows a simplier way, it will be appreciate.

