如何关闭所有活动并退出应用程序 [英] How to close all activities and exit from an app
问题描述
我的应用旨在仅允许登录用户访问活动.如果用户注销,则Shared首选项布尔值isLogged in设置为false,并且用户不应访问除LoginActivity之外的其他活动.
My app is intended to allow only logged in user to access the activities. If a user logs out, the Shared preference boolean isLogged in is set to false and the user should not access the rest of activities except the LoginActivity.
但是,我可以通过按后退"按钮访问以前打开的所有活动.
However, I am able to access all the previously opened activities by pressing the back button.
我将在打开每个活动时使用finish();
,但是我希望用户登录后仍然使用后退按钮.
I would use finish();
while opening each activity but then I would like users to still use the back button while they're logged in.
我尝试了其他类似问题的解决方案,例如
I have tried solutions from other similar questions like
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
,并且在我已添加的LoginActivity的onCreate()
上
and on the onCreate()
of my LoginActivity I have added
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
当我按下注销选项时,将打开上一个活动.
When I press the logout option, the previous activity opens instead.
有什么建议可以帮助我吗?
Any suggestions please help me?
推荐答案
您应使用此标志,以清除任务并创建一个新的标志.
You should use this flags, that clear your TASK and create a new one
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
这篇关于如何关闭所有活动并退出应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!