如何防止回到之前的活动?

发布于 2024-12-22 20:22:20 字数 444 浏览 7 评论 0原文

当按下手机上的“返回”按钮时,我想防止特定活动返回到上一个活动。

具体来说,我有登录和注册屏幕,当成功登录/注册时,都会启动一个名为 HomeScreen 的新活动。主屏幕启动后,我想阻止用户通过按“返回”键返回登录或注册屏幕。

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

我想过重写 HomeScreen 上的“后退”按钮的行为,以便在按下该按钮时直接完成应用程序,我使用了该按钮,

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

但这也不起作用。

When the BACK button is pressed on the phone, I want to prevent a specific activity from returning to its previous one.

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.

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.

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();
}

but that also does not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

月野兔 2024-12-29 20:22:20

我的建议是完成您不希望用户返回的活动。例如,在登录活动中,在调用 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.

梦归所梦 2024-12-29 20:22:20

以下解决方案在通常的登录/主要活动场景或实现阻塞屏幕中非常有用。

要最小化应用程序而不是返回到上一个 Activity,您可以像这样重写 onBackPressed()

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

moveTaskToBack(boolean nonRoot) 保持返回堆栈不变,只需将您的任务(所有活动)在后台进行。 与用户按下主页按钮相同

参数 boolean nonRoot - 如果为 false,则仅当活动是任务的根时才有效;如果为真,它将适用于任务中的任何活动。

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.

奶茶白久 2024-12-29 20:22:20

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

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

禁用 android 中的后退按钮

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

       return;
    }

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

  AndroidManifest.xml =>
<activity android:name=".SplashActivity" android:noHistory="true"/>

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

  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"/>
大海や 2024-12-29 20:22:20

对于您的情况,有两种解决方案:活动 A 启动活动 B,但您不想在活动 B 中返回活动 A。

1.从返回堆栈中删除了先前的活动 A。

    Intent intent = new Intent(activityA.this, activityB.class);
    startActivity(intent);
    finish(); // Destroy activity A and not exist in Back stack

2.禁用 Activity B 中的返回按钮操作。

有两种方法可以防止返回事件,

1) 推荐方法

@Override
public void onBackPressed() {
}

2)重写 onKeyDown 方法

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode==KeyEvent.KEYCODE_BACK) {
        return false;
    }
    return super.onKeyDown(keyCode, event);
}

希望它有用,但仍然取决于根据你的情况。

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.

零崎曲识 2024-12-29 20:22:20

由于已经提出了许多很好的解决方案,我将尝试给出更具代表性的解释。

如何跳过返回上一个活动?

从 Backstack 中删除前一个 Activity。简单的

如何从 Backstack 中删除之前的 Activity?

调用 finish() 方法

正常流程:

在此处输入图像描述
所有活动都存储在称为 Backstack 的堆栈中。
当您启动一个新的 Activity(startActivity(...)) 时,新的 Activity 会被推送到堆栈顶部,当您按下后退按钮时,该 Activity 会从堆栈中弹出。
需要注意的一个关键点是,当按下后退按钮时,会在内部调用 finish(); 方法。这是 onBackPressed() 方法的默认行为。

那么如果您想跳过活动 B 呢?

即A<---C

只需在 Activity B 中的 startActvity(...) 之后添加 finish(); 方法

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

在此处输入图像描述

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

少女情怀诗 2024-12-29 20:22:20

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

您是否尝试过 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | 的结合? Intent.FLAG_ACTIVITY_NO_HISTORY?请记住在Intent启动活动中使用此标志!

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.

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!

青衫儰鉨ミ守葔 2024-12-29 20:22:20

finish() 放在后面

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

Put finish() just after

Intent i = new Intent(Summary1.this,MainActivity.class);
            startActivity(i);
finish();
陈甜 2024-12-29 20:22:20

如果您不想返回应用程序上的所有活动,可以使用

android:launchMode="singleTask"

此处了解更多信息:

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

别想她 2024-12-29 20:22:20

paulsm4 的答案是正确的。如果在 onBackPressed() 中您只是返回,它将禁用后退按钮。但是,我认为考虑到您的用例,更好的方法是翻转活动逻辑,即将您的家庭活动作为主要活动,检查用户是否在那里登录,如果没有,则启动登录活动。原因是,如果您在主要活动中覆盖后退按钮,则大多数用户在按下后退而您的应用程序不执行任何操作时会感到困惑。

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.

用心笑 2024-12-29 20:22:20

这个方法运行良好

Intent intent = new Intent(Profile.this, MainActivity.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent);

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);
故事与诗 2024-12-29 20:22:20
@Override
public void onBackPressed() {
}

当您创建 onBackPressed() 时,只需删除 super.onBackPressed(); 就可以了

@Override
public void onBackPressed() {
}

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

云柯 2024-12-29 20:22:20

只需重写 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;
}

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;
}
洒一地阳光 2024-12-29 20:22:20

放在

finish();

紧接着
活动开始
停止活动,防止以任何方式返回活动。
然后添加

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

到您正在开始的活动中。

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.

爱冒险 2024-12-29 20:22:20

AndroidManifest.xml

 <activity 
      android:name=".welcome.SplashActivity"
      android:noHistory="true" // just add this line
      android:exported="true">  

 </activity>

AndroidManifest.xml

 <activity 
      android:name=".welcome.SplashActivity"
      android:noHistory="true" // just add this line
      android:exported="true">  

 </activity>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文