我在导航抽屉中使用LogOut menuitem。应用程序的流程如下,
如果按LogOut,我已编写代码来导航LogInActivity。
但它转移到 LogOut - > LogInActivity - > ShopList - > LogInActivity
LogOut Code如下,
if(id == R.id.nav_logout) {
commonUtil.dbUtil.open();
commonUtil.dbUtil.LogOut();
Intent moveToMain = new Intent(context, LogInActivity.class);
moveToMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
moveToMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
moveToMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(moveToMain);
MainActivity.this.finish();
}
工作正常:(将setFlags更改为addFlags后)
if (id == R.id.nav_logout) {
commonUtil.dbUtil.open();
commonUtil.dbUtil.LogOut();
Intent moveToMain = new Intent(context, LogInActivity.class);
moveToMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
moveToMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
moveToMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(moveToMain);
MainActivity.this.finish();
}
答案 0 :(得分:1)
尝试将setFlags
更改为addFlags
答案 1 :(得分:1)
这将清除堆栈中所有先前的活动
Intent moveToMain = new Intent(context, LogInActivity.class);
moveToMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(moveToMain);
答案 2 :(得分:1)
试试这个,
Intent moveToMain = new Intent(context, LogInActivity.class);
moveToMain .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(moveToMain);