從任意 Activity 返回主畫面時,使用 FLAG_ACTIVITY_CLEAR_TOP 清除堆疊上方所有 Activity,使按返回鍵直接離開應用。
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
onNewIntent 被呼叫。SINGLE_TOP 避免重複建立主 Activity。