When the BACK button is pressed on the phone, I want to prevent a specific activity from returning to its previous one.
当在电话上按下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.
具体来说,我有登录和注册屏幕,当成功登录/注册时,都会启动一个名为HomeScreen的新活动。启动HomeScreen后,我想通过按BACK键阻止用户返回登录或注册屏幕。
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.
我尝试使用Intent.FLAG_ACTIVITY_NO_HISTORY,但由于应用程序有Facebook集成,当使用“使用Facebook登录”时,Facebook应该返回初始登录屏幕,因此我应该保留这些活动的历史记录。
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
我想在HomeScreen上覆盖BACK按钮的行为,以便在按下按钮并且我使用时直接完成应用程序
@Override
public void onBackPressed() {
finish();
}
but that also does not work.
但这也行不通。
11 个解决方案
#1
287
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.
我的建议是完成您不希望用户返回的活动。例如,在您的登录活动中,在您调用startActivity之后,请调用finish()。当用户点击后退按钮时,他们将无法进入登录活动,因为它已从堆栈中杀死。
#2
87
Following solution can be pretty useful in the usual login / main activity scenario or implementing a blocking screen.
以下解决方案在通常的登录/主要活动场景或实现阻止屏幕时非常有用。
To minimize the app rather than going back to previous activity, you can override onBackPressed()
like this:
要最小化应用程序而不是返回到先前的活动,您可以像这样覆盖onBackPressed():
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
moveTaskToBack(boolean nonRoot)
leaves your back stack as it is, just puts your task (all activities) in background. Same as if user pressed Home button.
moveTaskToBack(boolean nonRoot)按原样离开你的后栈,只需将你的任务(所有活动)放在后台。与用户按下主页按钮相同。
Parameter boolean nonRoot
- If false then this only works if the activity is the root of a task; if true it will work for any activity in a task.
参数boolean nonRoot - 如果为false,则仅当活动是任务的根时才有效;如果为true,它将适用于任务中的任何活动。
#3
28
I'm not sure exactly what you want, but it sounds like it should be possible, and it also sounds like you're already on the right track.
我不确定你想要什么,但听起来应该是可能的,而且听起来你已经走在了正确的轨道上。
Here are a few links that might help:
以下是一些可能有用的链接:
Disable back button in android
在android中禁用后退按钮
MyActivity.java =>
@Override
public void onBackPressed() {
return;
}
How can I disable 'go back' to some activity?
如何禁用“返回”某些活动?
AndroidManifest.xml =>
<activity android:name=".SplashActivity" android:noHistory="true"/>
#4
8
There are two solutions for your case, like if activity A start to activity B, but you do not want to back to activity A in activity B.
您的案例有两种解决方案,例如,如果活动A开始活动B,但您不想返回活动B中的活动A.
1. Removed previous activity A from back stack.
1.从后堆栈中删除了之前的活动A.
Intent intent = new Intent(activityA.this, activityB.class);
startActivity(intent);
finish(); // Destroy activity A and not exist in Back stack
2. Disabled go back button action.
2.禁用返回按钮操作。
There are two ways to prevent go back event as below,
有两种方法可以防止返回事件,如下所示,
1) Recommend approach
1)推荐方法
@Override
public void onBackPressed() {
}
2)Override onKeyDown method
2)覆盖onKeyDown方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK) {
return false;
}
return super.onKeyDown(keyCode, event);
}
Hope that it is useful, but still depends on your situations.
希望它有用,但仍取决于你的情况。
#5
5
finish()
gives you method to close current Activity not whole application. And you better don't try to look for methods to kill application. Little advice.
finish()为您提供关闭当前Activity而非整个应用程序的方法。而你最好不要试图寻找杀死应用程序的方法。小建议。
Have you tried conjunction of Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_HISTORY
? Remember to use this flags in Intent
starting activity!
你有没有尝试过Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS |的结合Intent.FLAG_ACTIVITY_NO_HISTORY?记得在Intent开始活动中使用这个标志!
#6
2
paulsm4's answer is the correct one. If in onBackPressed()
you just return, it will disable the back button. However, I think a better approach given your use case is to flip the activity logic, i.e. make your home activity the main one, check if the user is signed in there, if not, start the sign in activity. The reason is that if you override the back button in your main activity, most users will be confused when they press back and your app does nothing.
paulsm4的答案是正确的。如果在onBackPressed()中你只是返回,它将禁用后退按钮。但是,我认为根据您的用例更好的方法是翻转活动逻辑,即将您的家庭活动作为主要活动,检查用户是否在那里登录,如果没有,则启动登录活动。原因是,如果您覆盖主活动中的后退按钮,大多数用户在按下时会感到困惑,而您的应用程序什么都不做。
#7
2
Put finish()
just after
刚刚完成完成()
Intent i = new Intent(Summary1.this,MainActivity.class);
startActivity(i);
finish();
#8
2
This method is working fine
这种方法工作正常
Intent intent = new Intent(Profile.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
#9
1
Just override the onKeyDown method and check if the back button was pressed.
只需覆盖onKeyDown方法并检查是否按下了后退按钮。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//Back buttons was pressed, do whatever logic you want
}
return false;
}
#10
1
If you don't want to go back to all the activities on your application, you can use
如果您不想返回应用程序上的所有活动,则可以使用
android:launchMode="singleTask"
Learn more here: http://developer.android.com/guide/topics/manifest/activity-element.html
点击此处了解详情:http://developer.android.com/guide/topics/manifest/activity-element.html
#11
0
Put
放
finish();
Immediately after ActivityStart to stop the activity preventing any way of going back to it. Then add
在ActivityStart之后立即停止活动,阻止任何方式返回它。然后加
onCreate(){
getActionBar().setDisplayHomeAsUpEnabled(false);
...
}
To the activity you are starting.
到您正在开始的活动。
#1
287
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.
我的建议是完成您不希望用户返回的活动。例如,在您的登录活动中,在您调用startActivity之后,请调用finish()。当用户点击后退按钮时,他们将无法进入登录活动,因为它已从堆栈中杀死。
#2
87
Following solution can be pretty useful in the usual login / main activity scenario or implementing a blocking screen.
以下解决方案在通常的登录/主要活动场景或实现阻止屏幕时非常有用。
To minimize the app rather than going back to previous activity, you can override onBackPressed()
like this:
要最小化应用程序而不是返回到先前的活动,您可以像这样覆盖onBackPressed():
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
moveTaskToBack(boolean nonRoot)
leaves your back stack as it is, just puts your task (all activities) in background. Same as if user pressed Home button.
moveTaskToBack(boolean nonRoot)按原样离开你的后栈,只需将你的任务(所有活动)放在后台。与用户按下主页按钮相同。
Parameter boolean nonRoot
- If false then this only works if the activity is the root of a task; if true it will work for any activity in a task.
参数boolean nonRoot - 如果为false,则仅当活动是任务的根时才有效;如果为true,它将适用于任务中的任何活动。
#3
28
I'm not sure exactly what you want, but it sounds like it should be possible, and it also sounds like you're already on the right track.
我不确定你想要什么,但听起来应该是可能的,而且听起来你已经走在了正确的轨道上。
Here are a few links that might help:
以下是一些可能有用的链接:
Disable back button in android
在android中禁用后退按钮
MyActivity.java =>
@Override
public void onBackPressed() {
return;
}
How can I disable 'go back' to some activity?
如何禁用“返回”某些活动?
AndroidManifest.xml =>
<activity android:name=".SplashActivity" android:noHistory="true"/>
#4
8
There are two solutions for your case, like if activity A start to activity B, but you do not want to back to activity A in activity B.
您的案例有两种解决方案,例如,如果活动A开始活动B,但您不想返回活动B中的活动A.
1. Removed previous activity A from back stack.
1.从后堆栈中删除了之前的活动A.
Intent intent = new Intent(activityA.this, activityB.class);
startActivity(intent);
finish(); // Destroy activity A and not exist in Back stack
2. Disabled go back button action.
2.禁用返回按钮操作。
There are two ways to prevent go back event as below,
有两种方法可以防止返回事件,如下所示,
1) Recommend approach
1)推荐方法
@Override
public void onBackPressed() {
}
2)Override onKeyDown method
2)覆盖onKeyDown方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK) {
return false;
}
return super.onKeyDown(keyCode, event);
}
Hope that it is useful, but still depends on your situations.
希望它有用,但仍取决于你的情况。
#5
5
finish()
gives you method to close current Activity not whole application. And you better don't try to look for methods to kill application. Little advice.
finish()为您提供关闭当前Activity而非整个应用程序的方法。而你最好不要试图寻找杀死应用程序的方法。小建议。
Have you tried conjunction of Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_HISTORY
? Remember to use this flags in Intent
starting activity!
你有没有尝试过Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS |的结合Intent.FLAG_ACTIVITY_NO_HISTORY?记得在Intent开始活动中使用这个标志!
#6
2
paulsm4's answer is the correct one. If in onBackPressed()
you just return, it will disable the back button. However, I think a better approach given your use case is to flip the activity logic, i.e. make your home activity the main one, check if the user is signed in there, if not, start the sign in activity. The reason is that if you override the back button in your main activity, most users will be confused when they press back and your app does nothing.
paulsm4的答案是正确的。如果在onBackPressed()中你只是返回,它将禁用后退按钮。但是,我认为根据您的用例更好的方法是翻转活动逻辑,即将您的家庭活动作为主要活动,检查用户是否在那里登录,如果没有,则启动登录活动。原因是,如果您覆盖主活动中的后退按钮,大多数用户在按下时会感到困惑,而您的应用程序什么都不做。
#7
2
Put finish()
just after
刚刚完成完成()
Intent i = new Intent(Summary1.this,MainActivity.class);
startActivity(i);
finish();
#8
2
This method is working fine
这种方法工作正常
Intent intent = new Intent(Profile.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
#9
1
Just override the onKeyDown method and check if the back button was pressed.
只需覆盖onKeyDown方法并检查是否按下了后退按钮。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//Back buttons was pressed, do whatever logic you want
}
return false;
}
#10
1
If you don't want to go back to all the activities on your application, you can use
如果您不想返回应用程序上的所有活动,则可以使用
android:launchMode="singleTask"
Learn more here: http://developer.android.com/guide/topics/manifest/activity-element.html
点击此处了解详情:http://developer.android.com/guide/topics/manifest/activity-element.html
#11
0
Put
放
finish();
Immediately after ActivityStart to stop the activity preventing any way of going back to it. Then add
在ActivityStart之后立即停止活动,阻止任何方式返回它。然后加
onCreate(){
getActionBar().setDisplayHomeAsUpEnabled(false);
...
}
To the activity you are starting.
到您正在开始的活动。