在我的Android应用中,我遇到导航问题。 要在我的应用程序上创建帐户,我有3个活动: A1-> A2-> A3。 当A3活动得到验证后,我将进入我登录的活动A4。 我想启用从A3到A2以及从A2到A1的历史导航,但是因为我已登录(A4),我不希望用户返回任何A3,A2,A1活动使用原生的android后退按钮。 如果我在A3上将noHistory设置为true,则A4活动上登录的用户仍然可以返回A2活动。 如果我在所有A1,A2,A3活动中将noHistory设置为true,即使用户未登录,用户也无法返回...
有人能告诉我最好的方法吗?
提前致谢!
答案 0 :(得分:1)
因此,我发现您的工作流程非常清晰,因此,制作干净的东西非常容易:
我会做这样的事情:
// --- I'm in your A4 activity, do not change anything for the other activities ----
boolean isUserLoggedIn;
// Modify the boolean when the user logs in
@Override
public void onBackPressed() {
if (isUserLoggedIn){
// Let's say you want the user to return at the device root menu at this point
new AlertDialog.Builder(this)
.setTitle("Really Exit?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}).create().show();
}
}
else{
// Should return to your previous activities if you set the history to true (what I recommend strongly)
super.onBackPressed();
}
}
我在Vi上快速写了(并没有测试),所以也许你会有一些小错误,但基本上就是这个想法。