1

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.

user999822
  • 445
  • 1
  • 9
  • 17
  • Can't you just finish C when you call D? and the same with the rest. Maybe take a look here: http://stackoverflow.com/questions/7706106/android-open-activity-and-close-previous-one – MAV May 28 '12 at 20:47
  • I think you should read this: http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html Especially the part about launch modes – Michał Klimczak May 28 '12 at 20:47
  • User can go back to C from D. normally My launcher activity is spalsh screen. I want to go back to the home activity. – user999822 May 28 '12 at 20:49
  • If you don't want an activity to be sitting in the activity stack you can just call `finish()` after calling intent. So when you leave your splash activity, call `finish()` and you will have your home activity at the bottom of the activity stack (the part occupied by your app) – Michał Klimczak May 28 '12 at 20:57
  • Thanks you. but my main problem is clearing both activity C and D from stack when user call E from D. The activity C and D can cleared both. because user can go C from D. – user999822 May 28 '12 at 21:01
  • You can do it in exactly the same way. See my answer. – Michał Klimczak May 28 '12 at 21:02

3 Answers3

2

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);
}
Jonas Gröger
  • 1,558
  • 2
  • 21
  • 35
  • OK. let say user jump activity F from activty E. if user press back on f. user can go to E. is it possible with your way ?. – user999822 May 28 '12 at 21:13
  • Yes. The Activity where you implement my snippet does not matter. It kills all Activities on top of B and brings B to front if it is in the Backstack. Just try it :) – Jonas Gröger May 28 '12 at 21:37
  • Ok. it worked. so how can ı close app on activity on when ı click back button – user999822 Jun 02 '12 at 19:08
1

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.

Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
  • I use a nagivation like facebook. http://qph.cf.quoracdn.net/main-qimg-3968c0cb69cb6d7cd27db1b6f0e87019 so user can jump the other activities from anyone. – user999822 May 28 '12 at 21:05
  • But this app doesn't override the back button. I have it and it implements the default model. I mean, the user can jump to any other activity, when he's moving forward, but pressing the back button gets the last activity fom the stack – Michał Klimczak May 28 '12 at 21:13
  • what is your advise ? Is there any solution ? – user999822 May 28 '12 at 21:21
  • Either you don't understand my solution, or I don't understand your question. If you override `onBackPressed()` in your activity C, D, E, F you can do whatever you want, including going back to B. I said it's wrong but it's not wrong as it is, but in principles. You shouldn't interfere the default behavior of back stack, but if it's what you really need to do - my answer is your solution. – Michał Klimczak May 28 '12 at 21:37
  • I understand. I should implement your way to C and E (I should go to home activity). Actvity D and F normally go back to C or E. Thanks – user999822 May 28 '12 at 21:45
  • Ok. it worked. so how can ı close app on activity on when ı click back button – user999822 Jun 02 '12 at 19:08
  • You should make sure that you cleared your activity stack and just call `finish()` in `onBackPressed()`. But, let me tell you again, what you're trying to do is totally against android usability patterns. Read this for example: http://stackoverflow.com/questions/2042222/close-application – Michał Klimczak Jun 02 '12 at 20:13
1

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.

jimotozka
  • 95
  • 1
  • 8