In the Android documentation I found what I was searching for: You can set the flag FLAG_ACTIVITY_CLEAR_TOP
to clear the back stack. The second flag FLAG_ACTIVITY_SINGLE_TOP
avoid restarting the activity if used in combination with the flag mentioned before.
case android.R.id.home: {
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivityIfNeeded(intent, 0);
return true;
}
The intent needs to be passed using startActivityIfNeeded()
.