Sudheer Kumar Palchuri
Sudheer Kumar Palchuri

Reputation: 2954

How to clear all existing activites in stack in Android

SignIn -> SignUp -> MainActivity, When i click back button in MainActivity, it should show Android Home Page, where as in my case it is moving to Login Screen. I tried below codes in SignUp Activity.

Option1:
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);          
    finish();

Option2:

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

Upvotes: 1

Views: 48

Answers (2)

romtsn
romtsn

Reputation: 11992

Try your second option, but with one adding:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

And it should work.

EDIT:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

Upvotes: 2

MrDumb
MrDumb

Reputation: 2178

add android:noHistory="true" in manifest to the activities you don't want to keep in activity stack. Also you can use onACtivityResult to achieve this purpose. After getting result call finish(). Hope this wil help you :)

Upvotes: 0

Related Questions