试试这个:
当您想CommunicationActivity从 Activity4 启动时,请改为执行以下操作:
Intent intent = new Intent(this, Activity1.class); // Your root activity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Will remove all other activities from the task
intent.putExtra("foo", true); // This is to tell Activity1 to launch Activity4
startActivity(intent);
在Activity1.onCreate()执行以下操作:
super.onCreate();
if (getIntent().hasExtra("foo")) {
// We should now launch CommunicationActivity and finish ourselves
Intent intent = new Intent(this, CommunicationActivity.class);
startActivity(intent);
finish();
return; // Don't continue the rest of onCreate()
}
// The rest of your onCreate() goes here
您只能用于CLEAR_TOP清除活动堆栈中的现有活动。这就是为什么你的使用CLEAR_TOP不起作用(因为CommunicationActivity在活动堆栈中不存在)。
此方法清除所有活动并再次启动根活动。当根活动onCreate()被调用时,它会检查 Intent 中是否有额外的“foo”,它会将其用作需要启动CommunicationActivity和完成自身的信号。这应该做你想要的。