user999822 user999822 - 1 year ago 172
Android Question

Android history stack clear without one

I have a some activities likr

A-> Splash Screen
B-> Home Screen
C-> a List activity
D-> a View activty
E-> a List activity
F-> a View activty

Normaly application start with A -> B

Then user select a list activity

then user select a view from list activity.

my problem is clearing activty stack.

my application will jump list or view activity to an activity (with facefook style navigation)

For example user select a list activity C and than select view activity D. at hat point user can jump to acitivty E. When in activity E, if user press o back button, user go to the activity D.

I want that to go to the activty B(Home).

I mean ı want to clear activity stact without home activity.

how can ı do this ?.

sory for awasome English.


Answer Source

You can intercept the back button press like so:

public void onBackPressed() {
    // You want to start B Activity
    Intent a = new Intent(this, B.class);

    // But you dont want it on top of E.

    // Launching this will now remove every activity on top of B and bring B to front, not! relaunch it.

You might want to check out the Android Intent flags and the Tasks and Back Stack Documentation.

Have a nice day!


If you want to close your app when back is pressed:

public void onBackPressed() {