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

半腔热情 提交于 2021-02-18 07:07:20

问题


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?


回答1:


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



回答2:


use

        ActivityCompat.finishAfterTransition(this);

This will finish activity after the animation




回答3:


Bundle options = ActivityOptionsCompat.makeCustomAnimation(this,R.anim.ani1,R.anim.ani2).toBundle();
ActivityCompat.startActivity(this, intent, options)


来源:https://stackoverflow.com/questions/15076617/how-to-animate-on-finish-via-activityoptions-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!