Niels Vanwingh
Niels Vanwingh

Reputation: 604

Delayed execution - Best Practices

In a particular fragment I am calling a number of fragment transactions on a button click.
Then an animation is started on the button.

I would like to have the animation running for 2 seconds before anything else happens.
There is a simple method in C# to do so, what it is the appropriate way for doing so in Java?

public class Collapsebutton_Fragment extends Fragment {

Collapsebutton_Fragment cpb_fragment;
Animation_Fragment anim_fragment;
FragmentManager fragmentManager;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.collapsebutton_fragment, container, false);

    //Look for an animation xml file with a good collapse animation
    final Animation collapse = AnimationUtils.loadAnimation(getActivity(),R.anim.collapse_animation);
    final Button button = (Button)rootView.findViewById(R.id.CollapseButton);
    cpb_fragment = new Collapsebutton_Fragment();
    anim_fragment = new Animation_Fragment();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.startAnimation(collapse);

            //wait for a certain time, then remove the button and start animation
            //remove is not functioning, why not?
            getFragmentManager().beginTransaction()
                    .remove(getFragmentManager().findFragmentById(R.id.CollapseButton))
                    .commit();

            //does not wait until the animation is finished
            getFragmentManager().beginTransaction()
                    .add(R.id.container_mainactivity,anim_fragment)
                    .addToBackStack(null)
                    .commit();
        }
    });

    return rootView;
}
}

Upvotes: 0

Views: 39

Answers (2)

meShakti
meShakti

Reputation: 273

You can put fragment code in a handler and use the handler's post delayed method In your onClickMethod You can write a handler and replace the Body of Code in run method .

final Handler handler = new Handler();
handler.postDelayed(new Runnable(){
    @Override
    public void run() {
        //Put your Fragment code here
    }
},2000L);

Upvotes: 2

Niels Vanwingh
Niels Vanwingh

Reputation: 604

Thank you @klingklang..

@Override
        public void onClick(View v) {
            v.startAnimation(collapse);
            collapse.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    //remove is not functioning, why not?
                    /*getFragmentManager().beginTransaction()
                    .remove(getFragmentManager().findFragmentById(R.id.CollapseButton))
                    .commit();*/

                    //repeat animation for 5 seconds, is not done yet
                    getFragmentManager().beginTransaction()
                            .add(R.id.container_mainactivity,anim_fragment)
                            .addToBackStack(null)
                            .commit();
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }
    });

Upvotes: 0

Related Questions