2013-03-21 137 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。