Here's my scenario - the app starts with an Activity A which is the main launching point. There are various other activity that are launched from A, say B & C. Going from A to B should be reversible (pressing back actually returning), so every I need A on the activity stack, same for A => C and back. All that works just fine by default.
Now here's where it gets tricky: I have another activity, say X, which can be launched from a background process (broadcast receiver). When X is launched, it should replace whatever is currently on the screen - that part is no problem. X can then either be dismissed by the user (which ends with finish() and startActivity(A) - so I don't have X on the stack). And it can also be finished by the background process (broadcast receiver receiving another intent) - and it is in the latter case where I have a problem.
I still call finish() on X and now the following happens: Whichever Activity was previously active (say A), will show again. That is fine if A showed before X launched, however, if my app was in the background, once X is dismissed I want the state prior to launching X restored (so either show the home screen, or show whichever app was running before X was launched). If you're wondering.. why am I interrupting the user - it's for a good reason (it's a telephony app and incoming calls should interrupt).
It seems to be that when I call dismiss on X, the app will return to whatever else is in the activity stack. That is fine if the app was in the foreground when X was launched, but if it wasn't, I want X to be dismissed and whichever app was active to be on top again.
Here are the intent flags I'm using when launching X: FLAG_ACTIVITY_NEW_TASK FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_NEW_TASK
I've already tried FLAG_ACTIVITY_MULTIPLE_TASK but the results were less than satisfactory (if my app was running in the background, this prevents it from coming on top when X is dismissed without user intervention, but then in the app history, it'll show X and X should never ever be shown there, plus if the user actually selects it, sure enough it launches X, and that's bad.
So briefly
Phone is in Status S1 X is launched X is dismissed without user interaction by calling finish() Phone should return to Status S1
Can this be achieved somehow?