问题描述
当按下手机上的BACK(返回)按钮时,我想防止特定活动返回到先前的活动.
When the BACK button is pressed on the phone, I want to prevent a specific activity from returning to its previous one.
具体来说,我有登录和注册屏幕,当成功登录/注册时,它们都会启动一个名为HomeScreen
的新活动.一旦启动HomeScreen,我想防止用户能够通过按BACK键返回登录或注册屏幕.
Specifically, I have login and sign up screens, both start a new activity called HomeScreen
when successful login/signup occurs. Once HomeScreen is started, I want to prevent the users from being able to return to the login or sign up screens by pressing the BACK key.
我尝试使用Intent.FLAG_ACTIVITY_NO_HISTORY
,但是由于该应用程序具有Facebook集成功能,因此当使用使用Facebook登录"时,Facebook应该返回到初始登录屏幕,因此我应该保留这些活动的历史记录.
I tried using Intent.FLAG_ACTIVITY_NO_HISTORY
, but since the application has Facebook integration, when the 'Login with Facebook' is used, Facebook should return to the initial login screen, therefore I should keep a history of these activities.
我想到了覆盖HomeScreen
上的BACK按钮的行为,以便在按下按钮并使用时直接完成应用程序
I thought of overriding the behaviour of the BACK button on HomeScreen
to directly finish an application when the button is pressed and I used
@Override
public void onBackPressed() {
finish();
}
但这也不起作用.
推荐答案
我的建议是完成您不希望用户返回的活动.例如,在登录活动中,在调用startActivity
之后,立即调用finish()
.当用户单击后退"按钮时,他们将无法进入登录活动,因为该活动已从堆栈中删除.
My suggestion would be to finish the activity that you don't want the users to go back to. For instance, in your sign in activity, right after you call startActivity
, call finish()
. When the users hit the back button, they will not be able to go to the sign in activity because it has been killed off the stack.
这篇关于如何防止回到上一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!