Pre Exam Test Practice (SSC | Banking | IAS | GATE | State Level | CLAT | Railways | Insurance | Teaching Exams | NDA | Defence | IES |State Exam )

Organizational Research By

Surprising Reserch Topic

how do you use intent flag activity clear top to clear the activity stack

how do you use intent flag activity clear top to clear the activity stack  using -'android,android-activity'

I've read through several posts about using this, but must be missing something as it's not working for me. My activity A has launchmode="singleTop" in the manifest. It starts activity B, with launchmode="singleInstance". Activity B opens a browser and receives and intent back, which is why it's singleInstance. I'm trying to override the back button so that the user is sent back to the activity A, and can then press Back to leave the activity, rather than back to activity B again.

// activity B
public boolean onKeyDown(int keyCode, KeyEvent event)  {
 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
  && keyCode == KeyEvent.KEYCODE_BACK
  && event.getRepeatCount() == 0) onBackPressed();
 return super.onKeyDown(keyCode, event);
public void onBackPressed() {
 startActivity(new Intent(this, UI.class)

After returning from the browser, the stack is...

I expect this code to change the stack to...
... so that pressing back once more takes the user back to the Home Screen.

Instead it seems to change the stack to...
A,B,Browser,B,A though those flags aren't there.

I tried calling finish() in activity B after startActivity, but then the back button takes me back to the browser again!

What am I missing? Thank you!

asked Sep 15, 2015 by SteLdo
0 votes

Related Hot Questions

Government Jobs Opening