How to finish an activity when transition is done Android

岁酱吖の 提交于 2019-12-07 22:58:17

问题


i want to end a previous activity after the transition between 2 activities is completed

i have tried ActivityCompat.finishAfterTransition(this); but it actually finishes the activity before the transition is done

i have looked into this solution before but i couldn't understand the answer to it , it would be swell if someone can explain how to end an activity after the transition is done

Updates :

i just tried starting the next activity like this

Intent intent = new Intent(LoginActivity.this, TaskActivity.class);
                    startActivity(intent);

the onStop code will be activated but when i use this

ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(LoginActivity.this);
                Intent intent = new Intent(LoginActivity.this, TaskActivity.class);
                startActivity(intent,options.toBundle());

the onStop wouldn't be activated so should i manually activate it and if so is it recommended


回答1:


The answer in the link you provided suggests to create a variable at the top of your activity

Boolean mShouldFinish = false;

Then after you start your transition to the next activity (after startActivity(ctx,intent, bundle)) add:

mShouldFinish = true;

And add override the onStop method with:

@Override
public void onStop() {
    super.onStop();
    if(mShouldFinish)
         finish();
}



回答2:


I have just shared my answer here. Use supportFinishAfterTransition();




回答3:


Guys found a solution to this, instead of using this code to the transition

if (Build.VERSION.SDK_INT >= 21) {
                    TransitionInflater inflater = TransitionInflater.from(LoginActivity.this);
                    Transition transition = inflater.inflateTransition(R.transition.fade_transition);
                    TransisitonTime = transition.getDuration()*2;
                    getWindow().setExitTransition(transition);
                }

                ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(LoginActivity.this);

i used this instead

overridePendingTransition(R.animator.fade_in,
                        R.animator.fade_out);

with these xml files

fade_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="2000"/>
</set>

fade_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="2000"/>
</set>

this way i can do the fade in and fade out transition when the finish() is called

kudos to @Droidman for pointing me in the right direction



来源:https://stackoverflow.com/questions/40317776/how-to-finish-an-activity-when-transition-is-done-android

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