How to finish Activity when starting other activity in Android?
You need to intent
your current context
to another activity first with startActivity
. After that you can finish
your current activity
from where you redirect.
Intent intent = new Intent(this, FirstActivity.class);// New activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
- Clears the activity stack. If you don't want to clear the activity stack. PLease don't use that flag then.
Intent i = new Intent(this,NewLaunchingActivity.Class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Call Only, if you wants to clears the activity stack else ignore it.
startActivity(i);
finish();
Add Intent Flag Intent.FLAG_ACTIVITY_CLEAR_TOP
if you want to clear the activity stack else ignore it. Read more about this here.