2

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

Community
  • 1
  • 1
Annonymous177
  • 563
  • 2
  • 10
  • 21
  • use ``finish()`` ? – Donald Wu Oct 29 '16 at 10:10
  • 2
    i would but it give a flickers and if i finish the activity at the next activity it will show the finish animation – Annonymous177 Oct 29 '16 at 11:04
  • are you looking for [overridePendingTransition](http://androidsourcehelp.com/simple-activity-animation-using-overridependingtransition-in-android/)? As fas as I understand your question this is pretty much what you want to achieve – Droidman Oct 29 '16 at 14:06
  • @Droidman nope not at all what i currently doing is a login screen and then when sign in is successful to fade in into the main activity but it does not make sense if the user can just press the back button and go back to the login screen – Annonymous177 Oct 29 '16 at 14:16
  • and from what i know from reading the android developer page the an activity will be stop when user change activity so that is why one http://stackoverflow.com/questions/29547439/android-shared-element-transitions-with-calling-activity-finish say to do the `finish()` on stop but this is not a good solution as having a phone call will also call the`on Stop()` code too – Annonymous177 Oct 29 '16 at 14:19
  • 1
    @Annonymous177 `the user can just press the back button and go back to the login screen` - you can prevent this behavior, either using `Intent` flags or declaring `noHistory` in the manifest. You will find a ton of examples when you [search](https://www.google.de/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=android%20prevent%20going%20back%20to%20previous%20activity) for it – Droidman Oct 29 '16 at 14:50

3 Answers3

4

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

ahmed_khan_89
  • 2,755
  • 26
  • 49
2

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();
}
Elias N
  • 1,430
  • 11
  • 19
0

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

Annonymous177
  • 563
  • 2
  • 10
  • 21