user999822
user999822

Reputation: 445

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.

Upvotes: 1

Views: 734

Answers (3)

Jonas Gröger
Jonas Gröger

Reputation: 1628

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);
}

Upvotes: 2

jimotozka
jimotozka

Reputation: 95

You can use tag android:noHistory="true" to activity elements at you AndroidManifest.xml file. This tag allow to not write activity in stack of activities.

Upvotes: 1

Michał Klimczak
Michał Klimczak

Reputation: 13154

If you want users to go back to B from all your activities, you can call finish() from every other activity when calling any Intent. This way, they'll be removed from activity stack.

Moreover you can override the back button behaviour:

@Override
public void onBackPressed() {
   Intent setIntent = new Intent(Intent.ACTION_MAIN);
   startActivity(setIntent);
}

But make sure it's really what you want. because it looks like you're doing something very bad for user experience. Read more on acitivty stack and tasks. Overriding the default behavior of back button should be avoided.

Upvotes: 1

Related Questions