1

我按以下順序呼叫活動A> B> C> D,現在我想打電話給活動A並清除B和C但保留D.我打電話A與Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP 。但除A之外的所有活動都已獲得批准。任何人都知道如何清除B和C並保持D> A。繼續致電活動並清除其他活動

Intent intent = new Intent(this, A.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
startActivity(intent); 

有沒有:在d這樣在你的清單

+0

你不能只是調用finish在B和C,如果你不需要他們了嗎? – joe

+0

我需要它,直到我打電話給D從D – Aju

+0

https://developer.android.com/reference/android/content/Intent.html嘗試閱讀這些標誌的含義,您可能會弄清楚何時使用哪一個 – joe

回答

2

您可以將移動到頂部(從d)任何簡單的標誌,你可以用來擺脫B和C.我建議你有B和C註冊一個BroadcastReceiver,聽一個特定的行動。從D啓動A後,您可以發送廣播Intent,這會導致B和C自行完成。例如,在B和C這樣做:

// Declare receiver as member variable 
BroadcastReceiver exitReceiver = new BroadcastReceiver() { 
    @Override 
    void onReceive (Context context, Intent intent) { 
     // This Activity is no longer needed, finish it 
     finish(); 
    } 
} 

onCreate()註冊接收器:

registerReceiver(exitReceiver, new IntentFilter(ACTION_EXIT)); 

不要忘記註銷接收器onDestroy()

在d,當您啓動A,發送廣播Intent含ACTION_EXIT使B和C完成,像這樣:

sendBroadcast(new Intent(ACTION_EXIT)); 
-2

添加此爲B類,C

<activity 
android:name=".AnyActivity" 
android:noHistory="true" />