安卓:preventing要回previous活动要回、preventing、previous

2023-09-04 23:48:18 作者:残魂断梦

我想这样做在Android上:基本上,当BACK按钮pssed在手机上$ P $,我想prevent一项具体活动,返回到其previous一个

I want to do this on Android: basically when BACK button is pressed on the phone, I want to prevent a specific activity to return to its previous one.

具体而言,我已登录并注册的屏幕,无论是启动一个名为主屏幕当成功登录/注册出现新的活动。一旦主屏幕开始,我想prevent的用户能够返回登录或者注册以pressing 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 to be able to return login or sign up screens with pressing 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 initial login screen, therefore I should keep a history of these activities.

我想到了后退按钮压倒一切的行为对主屏幕来直接完成的应用程序时,该按钮pssed $ P $和我用

I thought of overriding behaviour of BACK button on HomeScreen to directly finish application when the button is pressed and I used

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

但也不起作用。

but that also does not work.

我在Android开发很新,所以任何帮助将是非常有益的。

I am quite new in Android development, so any help would be very useful.

在此先感谢。

推荐答案

我的建议是完成,你不希望用户能够回到活动。例如,在你的星座在活动中,你调用之后 startActivity ,呼叫完成()。当用户点击后退按钮,他们将不能够去到活动的标志,因为它已经被杀死了堆栈。

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.