227

当在手机上按下 BACK 按钮时,我想防止特定活动返回到之前的活动。

HomeScreen具体来说,我有登录和注册屏幕,两者都会在成功登录/注册时启动一个新活动。启动 HomeScreen 后,我想阻止用户通过按 BACK 键返回登录或注册屏幕。

我尝试使用Intent.FLAG_ACTIVITY_NO_HISTORY,但由于应用程序集成了 Facebook,当使用“使用 Facebook 登录”时,Facebook 应该返回初始登录屏幕,因此我应该保留这些活动的历史记录。

我想在按下按钮并使用时覆盖 BACK 按钮的行为HomeScreen以直接完成应用程序

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

但这也行不通。

4

13 回答 13

366

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.

于 2011-12-28T18:53:08.397 回答
110

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:

@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.

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.

于 2014-10-21T17:34:27.487 回答
39

我不确定您到底想要什么,但听起来应该是可能的,而且听起来您已经走在正确的轨道上。

以下是一些可能有帮助的链接:

在android中禁用后退按钮

  MyActivity.java =>
    @Override
    public void onBackPressed() {

       return;
    }

如何禁用“返回”某些活动?

  AndroidManifest.xml =>
<activity android:name=".SplashActivity" android:noHistory="true"/>
于 2011-12-25T19:22:57.217 回答
22

There are two solutions for your case, activity A starts activity B, but you do not want to back to activity A in activity B.

1. Removed previous activity A from back stack.

    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 in activity B.

There are two ways to prevent go back event as below,

1) Recommend approach

@Override
public void onBackPressed() {
}

2)Override onKeyDown method

@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.

于 2016-10-26T10:59:36.280 回答
11

Since there are already many great solutions suggested, ill try to give a more dipictive explanation.

How to skip going back to the previous activity?

Remove the previous Activity from Backstack. Simple

How to remove the previous Activity from Backstack?

Call finish() method

The Normal Flow:

enter image description here
All the activities are stored in a Stack known as Backstack.
When you start a new Activity(startActivity(...)) then the new Activity is pushed to top of the stack and when you press back button the Activity is popped from the stack.
One key point to note is that when the back button is pressed then finish(); method is called internally. This is the default behavior of onBackPressed() method.

So if you want to skip Activity B?

ie A<--- C

Just add finish(); method after your startActvity(...) in the Activity B

Intent i = new Intent(this, C.class);
startActivity(i);
finish();

enter image description here

于 2019-04-15T11:01:06.023 回答
7

finish()为您提供关闭当前活动而不是整个应用程序的方法。而且你最好不要试图寻找杀死应用程序的方法。一点建议。

你试过连词Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_HISTORY吗?请记住在Intent开始活动时使用此标志!

于 2011-12-25T19:23:42.113 回答
4

Put finish() just after

Intent i = new Intent(Summary1.this,MainActivity.class);
            startActivity(i);
finish();
于 2015-04-17T18:19:08.597 回答
4

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

于 2015-10-28T15:27:30.060 回答
3

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.

于 2011-12-25T20:09:03.173 回答
3

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);
于 2017-03-25T06:45:19.607 回答
2

Just override the onKeyDown method and check if the back button was pressed.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if (keyCode == KeyEvent.KEYCODE_BACK) 
    {
        //Back buttons was pressed, do whatever logic you want
    }

    return false;
}
于 2015-03-30T12:49:09.193 回答
2
@Override
public void onBackPressed() {
}

When you create onBackPressed() just remove super.onBackPressed();and that should work

于 2018-12-07T05:25:00.667 回答
1

Put

finish();

immediately after ActivityStart to stop the activity preventing any way of going back to it. Then add

onCreate(){
    getActionBar().setDisplayHomeAsUpEnabled(false);
    ...
}

to the activity you are starting.

于 2014-11-02T18:13:28.237 回答