Skip to content
Advertisement

Finishing an Activity with transition shows the previous Activity from the activity stack first and then transitions to the new Activity

I have 2 Activities in my Android application – Activity A and Activity B. When the user presses a button on Activity A, he is navigated to Activity B. I want Activity A to close after going to the next activity and is completely removed from the activity stack. I tried adding intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); , but this not work as Activity A was still accessible after pressing the back button.

I added finishAffinity(); , this worked, but while transitioning, the previous activity in the activity stacks gets visible and then it goes to Activity B.

Video (here the application drawer is shown, and then it goes to Activity B) –

enter image description here

Code –

ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this);
Intent i = new Intent(MainActivity.this, MainActivity2.class);
startActivity(i, options.toBundle());
finishAffinity();

Any fix so that the previous activity is not visible and activity is also closed ?


EDIT : I tried finish(), finishAfterTransition() and supportFinishAfterTransition() instead of finishAffinity() but still the previous screen is visible. So I guess there’s an issue with finishAfterTransition()

Advertisement

Answer

I solved it by adding android:noHistory="true" attribute to the activity (which has to be removed from the stack) in the manifest file solves the issue. There is no need to use finish() in the java code after this change.

Method 2 – Refer @Pratyay ‘ s answer for a way around.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement