Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate on finish() via ActivityOptions in Android?

I'm working on an app that has two activities, Main and Info. The App is started with the MainActivity and when you click a button the InfoActivity slides in from the right side. When you click another button, InfoActivity shall slide out to the right side again and Main returns.

This is how I implemented the Animation and the Button Click in MainActivity:

buttonInfo.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) {
       Intent i = new Intent(getApplicationContext(), Info.class);
       Bundle mAnimate = 
          ActivityOptions.makeCustomAnimation(getApplicationContext(),
           R.anim.ani1,R.anim.ani2).toBundle();

          startActivity(i,mAnimate);
            }
        });

I did it similar in the InfoActivity, which works fine. However, i want and need to call finish() instead of startActivity with an intent, because I have a server connection in the MainActivity which disconnects when i call startActivity.

Any Ideas how to apply an animation like that to the finish() method or other suggestions?

like image 836
micha Avatar asked Feb 25 '13 21:02

micha


1 Answers

As explained in the DevBytes: Window Animations walkthrough, you can replace your Info.class's finish() method with

@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.ani2, R.anim.ani1);
}
like image 173
ianhanniballake Avatar answered Oct 11 '22 13:10

ianhanniballake