Reputation: 63
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) -
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()
Upvotes: 0
Views: 666
Reputation: 63
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.
Upvotes: 2
Reputation: 106
@Override
public void onBackPressed() {
finishAffinity();
}
Use this function in Activity B, this will exit app on pressing back button. And don't use any finish();
or finishAffinity();
in Activity A. This will give a smooth transition
Upvotes: 0
Reputation: 212
you have to use finish but like this:
startActivity(Your Activity here)
then Finish()
if you do reverse you can see black screen for a short time do this way
Upvotes: 0