从其他活动完成活动
答
当您打开C活动时使用此标志。
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
这将清除C.
答
的顶部由于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。
对我来说听起来像A和B在** C下(即:在C之前),而不是在C之下。在这种情况下,FLAG_ACTIVITY_CLEAR_TOP将无济于事。 – 2013-03-21 20:51:47