Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 497
Beloo Avatar asked Dec 20 '22 09:12

Beloo


1 Answers

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;
}
like image 130
Thomas Avatar answered Dec 27 '22 11:12

Thomas