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

micha picture micha · Feb 25, 2013 · Viewed 7.6k times · Source

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?

Answer

ianhanniballake picture ianhanniballake · Feb 25, 2013

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);
}