这个决定很好用:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
但是新活动启动时间很长,一段时间后您会看到白屏。如果这很关键,请使用以下解决方法:
public class BaseActivity extends AppCompatActivity {
private static final String ACTION_FINISH = "action_finish";
private BroadcastReceiver finisBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(finisBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
}, new IntentFilter(ACTION_FINISH));
}
public void clearBackStack() {
sendBroadcast(new Intent(ACTION_FINISH));
}
@Override
protected void onDestroy() {
unregisterReceiver(finisBroadcastReceiver);
super.onDestroy();
}
}
使用方法:
public class ActivityA extends BaseActivity {
// Click any button
public void startActivityB() {
startActivity(new Intent(this, ActivityB.class));
clearBackStack();
}
}
缺点:必须在堆栈上关闭的所有活动都必须扩展BaseActivity