4

Hi I am developing android application in which I have activity flow like this A-->B-->C.

So my intention is that as soon as B opens activity C close both A and B. For that I am trying to use 2 flags: Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP

But when I click back it shows me both activities B and A. I start activity like this

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);

I don't know why it is not working?Am I doing anything wrong? Need Help. Thank you.

silvia_aut
  • 1,481
  • 6
  • 19
  • 33
nilkash
  • 7,408
  • 32
  • 99
  • 176
  • Try this `Intent intent = new Intent(B.this, C.class) intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);` – Raghunandan Oct 11 '13 at 06:56
  • 2
    It is not working the way you want because, the flag `CLEAR_TOP` will clear any activity that is on top the activity being launched. IN your case `C` is being launched and there is nothing on top of `C` because there is no pre existing instance of `C`. Please read the documentation carefully.. – Varun Oct 11 '13 at 07:00

3 Answers3

2

public static final int FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

Intent a = new Intent(this,A.class);
        a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(a);

For more information look here

Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30
1

I know its a bit late, but for anyone else who's seeking the solution, calling finish() did the trick.

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);
    finish();
Milan Maharjan
  • 4,156
  • 1
  • 21
  • 28
0

remove Intent.FLAG_ACTIVITY_SINGLE_TOP

Manivannan
  • 1,630
  • 1
  • 11
  • 14
  • Thank you for the quick help. I tried that one as well but still not working. Any thing else :(. – nilkash Oct 11 '13 at 06:59