0

I have 3 activities: A. B ,...., C From A: Click open B. From B: click open C

At C: i want close all activities (B,..,C) and back to A: I use this code, but it only close activity C, not close activities (B,...) :

  Intent intent = new Intent(getApplicationContext(), A.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    Bundle b = new Bundle();
    b.putBoolean("UpdateVersion", true);
    intent.putExtras(b);
    startActivity(intent);

Why can't close all activities?

D T
  • 3,522
  • 7
  • 45
  • 89
  • 1
    You need to read about Effective Navigation in android. You should not clear the back stack. Back button is supposed to take you back to the previous activity. Your design is wrong. Check this http://developer.android.com/design/patterns/navigation.html – Raghunandan Jun 13 '14 at 11:54
  • +1 for the above comment. but if such is your requirement, then just call finish() after startActivity(intent) where ever you prefer. if you want to keep an activity(say B) then don't include the finish() at the end. – SKK Jun 13 '14 at 12:06

2 Answers2

2

Try this..

After starts from B Activity use finish()

startActivity(intent);
finish();
Hariharan
  • 24,741
  • 6
  • 50
  • 54
1

You need to look into calling startActivityForResult() so that on the callback of the created Activity of C, the previous Activity of B gets finished as well. The callback function is called onActivityResult(), and in that, you want to call finish().

Example:

ActivityB:

Intent i = new Intent(this, ActivityC.class);
startActivityForResult(i, 0);

...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    this.finish();
}

ActivityC:

//do stuff

This way, when you press Back (or call finish()) in ActivityC, it will call back on ActivityB's onActivityResult() function, and it will end them both.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428