我有九个活动。例如,活动“A”是主要的。用户从NavigationDrawer打开活动“B”,“С”,“D”,在“D”中他点击“返回”按钮。在这种情况下,需要返回到活动“A”,而不是通过最近打开的活动的整个堆栈。将活动保留在堆栈中,但只返回主
此代码做什么,我需要:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Handler mHandler = new Handler();
drawerLayout.closeDrawers();
switch (item.getItemId()) {
case R.id.A:
if (item.isChecked()) {
break;
} else {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
intent = new Intent(activity, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
activity.startActivity(intent);
}
}, 250);
}
break;
case R.id.B:
if (item.isChecked()) {
break;
} else {
if (activity instanceof A) {
userFlavorsMap.clear();
if (flavorContainer.getChildCount() > 0){
flavorContainer.removeAllViews();
}
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
intent = new Intent(activity, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
activity.startActivity(intent);
}
}, 250);
}
break;
case R.id.C:
if (item.isChecked()) {
break;
} else {
if (activity instanceof A) {
userFlavorsMap.clear();
if (flavorContainer.getChildCount() > 0){
flavorContainer.removeAllViews();
}
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
intent = new Intent(activity, C.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
activity.startActivity(intent);
}
}, 250);
}
break;
case R.id.D:
if (item.isChecked()) {
break;
} else {
if (activity instanceof A) {
userFlavorsMap.clear();
if (flavorContainer.getChildCount() > 0){
flavorContainer.removeAllViews();
}
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
intent = new Intent(activity, D.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
activity.startActivity(intent);
}
}, 250);
}
break;
case R.id.E:
if (item.isChecked()) {
break;
} else {
if (activity instanceof A) {
userFlavorsMap.clear();
if (flavorContainer.getChildCount() > 0){
flavorContainer.removeAllViews();
}
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
intent = new Intent(activity, E.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
activity.startActivity(intent);
}
}, 250);
}
break;
case R.id.F:
File usersCounter = new File("data/data/com.com.com/shared_prefs/file.xml");
if (usersCounter.exists()) {
if (item.isChecked()) {
break;
} else {
if (activity instanceof A) {
userFlavorsMap.clear();
if (flavorContainer.getChildCount() > 0){
flavorContainer.removeAllViews();
}
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
intent = new Intent(activity, F.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
activity.startActivity(intent);
}
}, 250);
}
break;
} else {
if (item.isChecked()) {
break;
} else {
if (activity instanceof A) {
userFlavorsMap.clear();
if (flavorContainer.getChildCount() > 0){
flavorContainer.removeAllViews();
}
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
intent = new Intent(activity, G.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
activity.startActivity(intent);
}
}, 250);
}
break;
}
}
return false;
}
但是,如果用户切换到活动,例如,“C”和折叠的应用程序,然后返回到应用程序后,会打开“A”。我怎样才能完成代码,以便当我返回到应用程序时,它打开上次打开的Activity,但所有最近的活动都不会在堆栈中?
在manifect 机器人:launchMode =“singleTop”
覆盖onBackPressed方法,并开始主要活动有 –
如果你不希望能够去回到某些活动中,当你打开下一个活动时,“完成()”它们。所以,当你按下后,他们不在堆栈中,只有A. – Doomsknight