简体   繁体   English

如何通过按后退按钮关闭非root活动的Android应用程序

[英]How to close an Android app from non-root activity by pressing the backbutton

The functionality I want to model in my Android app is, that if the user does login, he gets forwarded to the main menu ( MainMenuActivity ). 我想在我的Android应用程序中建模的功能是,如果用户登录,他将被转发到主菜单( MainMenuActivity )。 From here and everywhere else where I provide a button for it in the app, he must be able to logout, which should send him back to the login screen ( LoginActivity ) and finish all the activities above on the stack. 从这里和我在应用程序中为其提供按钮的其他地方,他必须能够注销,这应该将他发送回登录屏幕( LoginActivity )并完成堆栈上面的所有活动。 I achieve this by not destroying the LoginActivity after a login and at logout calling: 我通过在登录和注销后不破坏LoginActivity来实现这一点:

    Intent intent = new Intent(this, LoginActivity.class);
    // Clean up all other activities.
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(intent);

Also I want that the user can not go back with the backbutton form the main menu to the loginscreen. 此外,我希望用户不能使用主菜单的backbutton返回到登录屏幕。 So I did overwrite the method onBackPressed() in my MainMenuActivity . 所以我在MainMenuActivity覆盖了onBackPressed()方法。 Instead I want that when the user is pressing the backbutton from the main menu, the app does terminate (just like its the last activity on the stack). 相反,我希望当用户从主菜单按下后退按钮时,应用程序会终止(就像它在堆栈上的最后一个活动一样)。 How can I achieve this? 我怎样才能做到这一点?

I did read about several other approaches, for example finishing the LoginActivity after the login was performed and then later do it like described here: 我确实阅读了其他几种方法,例如在执行登录后完成LoginActivity,然后再按照此处所述进行操作:

https://stackoverflow.com/a/3008684/1332314 https://stackoverflow.com/a/3008684/1332314

But I'm not quite sure if this is a proper solution, just read the comments below the post. 但我不太确定这是否是一个合适的解决方案,只需阅读帖子下方的评论。

Apparently I did find a solution to my problem. 显然我确实找到了解决问题的方法。 I just start the MainMenuActivity from the LoginActivity like this: 我只是从LoginActivity启动MainMenuActivity ,如下所示:

private void continueToMainMenu() {
    Intent intent = new Intent(this, MainMenuActivity.class);
    startActivityForResult(intent, 0);
}

Then, when the user presses the backbutton in the main menu, this overwritten function gets called: 然后,当用户按下主菜单中的后退按钮时,将调用此覆盖的函数:

public void onBackPressed() {
    setResult(LoginActivity.ACTION_QUIT);
    finish();
}

This will at first set a resultcode for the LoginScreen and then finish the MainMenuActivity . 这将首先为LoginScreen设置结果代码,然后完成MainMenuActivity Then the LoginActivity does terminate receiving this particular resultcode, which I implemented like this: 然后LoginActivity终止接收这个特定的结果代码,我实现如下:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == LoginActivity.ACTION_QUIT)
    {
        finish();
    }
}

Doing it this way will ensure not to touch too deep into the activity lifecycles, all the callbacks to clean up will still be called and it should not open any security leaks. 这样做将确保不会触及活动生命周期太深,所有清理回调仍将被调用,并且不应该打开任何安全漏洞。

Hope this helps if anyone else will bump into the same issue. 希望如果其他人遇到同样的问题,这会有所帮助。

use this flag when you move to MainMenuActivity : 移动到MainMenuActivity时使用此标志:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);

this will delete all the previous activity and if you press back app will be closed. 这将删除所有以前的活动,如果您按后退应用程序将被关闭。

Now in case of logout button do the same thing when you move to LoginActivity same flag will now make LoginActivity as the only activity on the stack and on back app will be closed. 现在,在注销按钮的情况下,当您移动到LoginActivity时, LoginActivity相同的操作,同一标志现在将使LoginActivity成为堆栈上的唯一活动,并且将关闭后台应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM