2013-03-21 149 views
2

假設我有3項活動A,B和C.A導致B導致C.我希望能夠在A和B之間前後移動,但是我想完成A和B一旦C開始。我知道如何通過意圖啓動C時關閉B,但我如何在C啓動時關閉A?從其他活動完成活動

回答

1

當您打開C活動時使用此標誌。

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

這將清除C.

+1

對我來說聽起來像A和B在** C下(即:在C之前),而不是在C之下。在這種情況下,FLAG_ACTIVITY_CLEAR_TOP將無濟於事。 – 2013-03-21 20:51:47

0

的頂部由於A所有的活動是你的根(起點)的活性,可以考慮使用A作爲調度員。如果要啓動C並完成所有其他活動(下)之前,這樣做:

// Launch ActivityA (our dispatcher) 
Intent intent = new Intent(this, ActivityA.class); 
// Setting CLEAR_TOP ensures that all other activities on top of ActivityA will be finished 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
// Add an extra telling ActivityA that it should launch ActivityC 
intent.putExtra("startActivityC", true); 
startActivity(intent); 

ActivityA.onCreate()做到這一點:

super.onCreate(); 
Intent intent = getIntent(); 
if (intent.hasExtra("startActivityC")) { 
    // Need to start ActivityC from here 
    startActivity(new Intent(this, ActivityC.class)); 
    // Finish this activity so C is the only one in the task 
    finish(); 
    // Return so no further code gets executed in onCreate() 
    return; 
} 

這裏的想法是,你推出ActivityA(您的調度員)使用FLAG_ACTIVITY_CLEAR_TOP,以便它是該任務中的唯一活動,並告訴它您想要啓動的活動。然後它將啓動該活動並完成自己。這將使您只在Activity中留下ActivityC。