user999822 user999822 - 6 months ago 57
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.

Thanks.

Answer

You can intercept the back button press like so:

@Override
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.
    a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    // Launching this will now remove every activity on top of B and bring B to front, not! relaunch it.
    startActivity(a);
}

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

Have a nice day!

EDIT:

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

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}
Comments