You can achieve that by using BroadcastReceivers
:
- Create a
BaseActivity
like this:
public class BaseActivity extends Activity {
private KillReceiver mKillReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mKillReceiver = new KillReceiver();
registerReceiver(mKillReceiver,
IntentFilter.create("kill", "spartan!!!"));
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mKillReceiver);
}
private final class KillReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
}
}
- Make your activities extend that
BaseActivity
.
- Whenever you want to clear the stack:
Intent intent = new Intent("kill");
intent.setType("spartan!!!");
sendBroadcast(intent);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…