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
@Override
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);
}
@Override
public void onBackPressed() {
startActivity(new Intent(this, UI.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
return;
}
After returning from the browser, the stack is... A,B,Browser,B
I expect this code to change the stack to... A ... 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 ...as 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!
@bitestar has the correct solution, but there is one more step:
It was hidden away in the docs, however you must change the
launchMode
of theActivity
to anything other thanstandard
. Otherwise it will be destroyed and recreated instead of being reset to the top.FLAG_ACTIVITY_NEW_TASK is the problem here which initiates a new task .Just remove it & you are done.
Well I recommend you to read what every Flag does before working with them
Read this & Intent Flags here
For this, I use
FLAG_ACTIVITY_CLEAR_TOP
flag for startingIntent
(without
FLAG_ACTIVITY_NEW_TASK
)and
launchMode = "singleTask"
in manifest for launched activity.Seems like it works as I need - activity does not restart and all other activities are closed.
Initially I also had problem getting FLAG_ACTIVITY_CLEAR_TOP to work. Eventually I got it to work by using the value of it (0x04000000). So looks like there's an Eclipse/compiler issue. But unfortunately the surviving activity is restarted, which is not what I want. So looks like there's no easy solution.