Android 反压无效

Android backpressed not working

我正在尝试在 android 中构建一个简单的 login & registration 应用程序。我的问题是我想处理 backpressed() 但它不起作用。

Example: I'm having 3 activities: signin, register and verify. What I need is if we back click on register or verify, the navigation should go to signin and if we back click on signin, the application should close.

我已经尝试了这些代码行,但它们不起作用。他们将导航到之前访问过的 activity。 此代码是 signin activity

@Override
public void onBackPressed() {
    Intent i = new Intent();
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    finish();
}

此代码属于寄存器activity

@Override
public void onBackPressed() {
    Intent i = new Intent(getBaseContext(), LoginActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
}

如果您要为 API>16

建造
@Override
public void onBackPressed(){
  finishAffinity();
  super.onBackPressed();
}

What I need is that if we back click on register or verify, the navigation should go to signin and if we back click on signin, the application should close.

要从 VerifyRegisterActivity 转到 SignInActivity onBackpressd()

@Override
public void onBackPressed() {
    Intent i = new Intent(this, SignInActivity.class);
    finishAffinity();
    startActivity(i);
}

SignInActivity中:

@Override
    public void onBackPressed() {
        finish();            
    }

希望对您有所帮助。

这肯定有效!!

   @Override
    public void onBackPressed() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finishAffinity();
        System.exit(0);


    }

Finish 将始终转到堆栈中的前一个 activity。意图标志需要在您的 activity 启动时设置,而不是在您的 activity 被杀死时设置。