Removing Activity from BackStack (Flag_Activity_no_history) does not work

6.5k views Asked by At

I've a Start button in my MainActivity. If I click on this button I go to the next Activity (InfoActivity). Now, I want to remove the MainActivity from the BackStack, if I click on the button. I've tried this:

View.OnClickListener startButtonListener = new View.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, InfoActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intent);
    }
};

But that removes not the MainActivity from the BackStack, it removes the InfoActivity from the BackStack.

I know that I could insert the flag into the AndroidManifest. But that is not possible for me, because if I go to the PreferencesActivity from the MainActivity and I using the flag in the AndroidManifest, the MainActivity is removed if I return from the PreferencesActivity back to the MainActivity.

So I want to remove the MainActivity from the BackStack, if I click on the button Start.

2

There are 2 answers

0
Apurva Kolapkar On BEST ANSWER

Add finish(); Like this :

View.OnClickListener startButtonListener = new View.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, InfoActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intent);
        this.finish(); 
    }
};
0
X3Btel On

As said you should use Intent.FLAG_ACTIVITY_NO_HISTORY on the activity you dont want to track in your case on MainActivity. Other way to do it is Intent.FLAG_ACTIVITY_CLEAR_TOP on your Info activity this will clear whole history (even things before MainActivity)