1

Let's Suppose that I have the Sequence of the following Activities :

A -> B -> C -> D -> E

when I be in E coming from D I want to do some Actions then Go back to D without Keeping the E in the Stack.. So if I have back to D and press Back Button I will return to C not E

how to do that ???

Mohamad Ghanem
  • 599
  • 2
  • 8
  • 25

5 Answers5

1

If you add this in your android manifest for activity E it will not appear in the History stack.

android:noHistory="true"
Mazvél
  • 951
  • 1
  • 8
  • 22
0

I'm not sure if I completely understand this

if I have back to D and press Back Button I will return to C not E

but if I do all you have to do is call finish() when you are done in E then you will return to D, then to C when you press back in D. If this is not what you are talking about then please clarify your question. But, when you finish() and Activity then it is cleared off of the stack so you would never return to E until you start it again. You never would from D anyway by pressing the back button

Google I/O Navigation

In its most basic form, Activities are put on the stack on top of each other in the way they come in. If you leave one, by calling finish() or pressing back, then it is removed and you are taken to the one placed before it (where you came from). There is much more to that but that is the very basic of what happens.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I mean with that , After I return from E to D then If I press Back but I should back to C ,,, where to call finish Function ?? – Mohamad Ghanem Jul 17 '13 at 01:39
  • When you are done in E you can call `finish()`. However, if you press back in E it will do the same thing. If you then press back in D you WILL return to C assuming you haven't cleared it off of the stack somehow – codeMagic Jul 17 '13 at 01:40
  • NO ,, I want to leave E by using Intent not by pressing Back button – Mohamad Ghanem Jul 17 '13 at 01:42
  • Why do you want to use `Intent`? If you haven't cleared D off the stack by calling `finish()` then you don't need an `Intent` to get there – codeMagic Jul 17 '13 at 01:43
  • 1
    The link I just posted in my answer is long but worth watching – codeMagic Jul 17 '13 at 01:43
  • Thanks for your help,, so all i need is to call 'finis()' when I'm done in E and it's return to D without keeping E in stack Right ? – Mohamad Ghanem Jul 17 '13 at 01:46
  • 1
    That is correct, sir. Do yourself a favor and watch that video though. It is very informative – codeMagic Jul 17 '13 at 01:48
0

Instead of creating a new intent to go back you should call this.finish(); on the activities that you no longer want, this will be "called" when the user pressed the back button as well, also, if you are creating a new activity and you wish for the current activity to be skipped when pressing back you can still call this.finish(); and then you can call this.startActivity(new Intent(this, ActivityName.class));

FabianCook
  • 20,269
  • 16
  • 67
  • 115
0

I think this is how you did your activity stack

A -> B -> C -> D -> E -> D (launched by intent from E).

Now you want to go from the second D to C. To do that, in E, call finish() after you start D. This should remove E from the stack.

iTurki
  • 16,292
  • 20
  • 87
  • 132
-1

If I understood your problem correctly;

You goes to Activity-E in a sequence of A, B, C, D, E. Now you will come at the Activity-D when you will press the back button. And here is the problem: When you press again back button, it goes again to Activity-E but you want to go on Activity-C here.

So you can use it in Activity E

@Override
public void onBackPressed(){
    finish() ;            // ActivityE.this.finish()
    super.onBackPressed() ;
}
Husnain Iqbal
  • 466
  • 6
  • 16