Android 导航提示

Android Navigation tips

在我的 Android 应用程序中,我遇到导航问题。 要在我的应用程序上创建一个帐户,我有 3 个活动: A1->A2->A3。 验证 A3 activity 后,我将转到我登录的 activity A4。 我想启用从 A3 到 A2 以及从 A2 到 A1 的历史导航,但由于我已登录 (A4),我不希望用户使用本机 android 后退按钮。 如果我在 A3 上将 noHistory 设置为 true,则在 A4 activity 上登录的用户仍然可以在 A2 activity 上返回。 如果我在所有 A1、A2、A3 活动上将 noHistory 设置为 true,即使用户未登录也无法返回...

谁能告诉我最好的方法是什么?

提前致谢!

所以,我发现你的工作流程非常清晰,因此,很容易使一些东西变得干净:

我会这样做:

// --- 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 上快速写的(并没有测试),所以你可能会有一些小错误,但基本上就是这个想法。