Hacky but 100% working solution: You can send a Broadcast
that all activities are waiting for and that calls finish()
on them.
private String action = "clear";
private String type = "content://whatever_you_like"; //You should read stuff about this because it's a hack..
onCreate of each Activity:
clearStackManager = new ClearStackManager();
registerReceiver(clearStackManager,
IntentFilter.create(action, type));
Then define it:
private final class ClearStackManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
}
onDestroy:
unregisterReceiver(clearStackManager);
Calling it:
public void clearStack() {
Intent intent = new Intent(action);
intent.setType(type);
sendBroadcast(intent);
}
Out of the box solution: Call by intent the first activity of the stack (if it's always the same) with FLAG CLEAR_TOP (removing all activities except that one) and then on onNewIntent finish the last one.
I dunno if it works solution: I also found this: https://stackoverflow.com/a/6403577/327011 but i never worked with actions, so i'm not sure what will happen if multiple activities have the same action.
UPDATE:
You should use LocalBroadcastManager instead of "global" Broadcasts to avoid sending global broadcasts.
http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…