If I understand you correctly you are saying that you have N number of the SAME activity and wish to go back to some arbitrary starting point activity in the stack?
I am not totally clear on whether you know ahead of time that all activities forward of your "sticky" activity should be finished or if this is determined somewhere along the way
I don't know of any arbitrary way of popping N activities off the stack when they are duplicate activities but you could roll your own, such as (not finished code)
If you know ahead of time that A(M) will be sticky before it launches the next activity(s) you could just tell each forward activity that it needs to kill itself once it launches it's new task by passing it a flag then when the last activity in the chain ends you are back to A(M)
in A(...)
startSelf()
{
Intent I = new Intent(this,MyActivity.class);
if (bFinishSelf || bFinishForward)
I.putExtra("finishself", 1);
if (Finishelf)
finish();
startActivity(I);
in ... all A(...) onCreate( ...
Bundle b = getIntent().getExtras();
if(b !=null && extras.getInt("finishself");
bFinishSelf = true;
Then when you start a new activity check if the finishself flag is set and if so call finish() after starting new activity ...
If on the other hand A(z) is the one that determines you need to return to A(...) then I guess in A(z) you could tell the calling parent(s) that they need to kill themselves until reaching some ID contained in data ...
A(M)
startActivityForResult(...)
A(Z) somehow you determine that A(M) should be the sticky activity ...
finshAndStopAt(int ID) {
Intent I = new Intent();
I.putExtra("finish",ID);
if (getParent() == null) {
setResult(Activity.RESULT_OK, I);
} else {
getParent().setResult(Activity.RESULT_OK, I);
}
finish();
}
All A(z ... m) (parent's) would monitor for the return data
@Override void onActivityResult(int requestCode, int resultCode, Intent data) {
// If we are asked to finish ourselves, pass it on ...
if (null != data && data.getInt("finish",stopAt) != myID) {
if (getParent() == null) {
setResult(Activity.RESULT_OK, data);
} else {
getParent().setResult(Activity.RESULT_OK, data);
finish();
}
// else we can hang around and are the sticky activity
}
It's a bit of pain and maybe someone knows of an easier method