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

发布于 2024-11-15 22:48:36 字数 517 浏览 2 评论 0原文

我不希望用户能够返回到我的应用程序的启动屏幕。一种解决方案似乎是检查当前活动下方的活动是否是闪屏的实例,在这种情况下退出应用程序,如下面的代码所示。但是,我不知道如何检查堆栈中的前一个活动是什么。有人可以帮忙吗?有没有其他方法可以禁用“返回”给定活动?

@Override
public void onBackPressed() { 
    if(<previous activity in stack is an instance of splashscreen>){   
        Intent exit_intent=new Intent(CurrentActivity.this, SplashScreen.class);
        exit_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        exit_intent.putExtra("EXIT", true);
        context.startActivity(exit_intent);
    }
}

I don't want the user to be able to go back to the splashscreen of my app. One solution seems to be to check if the activity below the current one is an instance of the splashscreen, and in that case exit the app, as shown in the code below. However, I don't know how to check what's the previous activity in the stack. Anybody can help? Is there any other way to disable 'go back' to a given activity?

@Override
public void onBackPressed() { 
    if(<previous activity in stack is an instance of splashscreen>){   
        Intent exit_intent=new Intent(CurrentActivity.this, SplashScreen.class);
        exit_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        exit_intent.putExtra("EXIT", true);
        context.startActivity(exit_intent);
    }
}

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

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

发布评论

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

评论(4

微凉 2024-11-22 22:48:36

开始下一个活动后,立即在启动屏幕活动中调用 finish()

另一种方法是将此属性添加到 AndroidManifest.xml 中的 Activity:android:noHistory="true"

示例:

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

此属性指示 Android 从历史记录堆栈中删除 SplashActivity 一次它导航远离。

Call finish() in your Splash Screen activity right after starting the next activity.

Another approach is to add this attribute to your activity in AndroidManifest.xml: android:noHistory="true"

Example:

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

This attribute instructs Android to remove SplashActivity from the history stack once its navigated away from.

苏璃陌 2024-11-22 22:48:36

只需在 context.startActivity() 之后调用 context.finish()

Just call context.finish() after context.startActivity()

天煞孤星 2024-11-22 22:48:36

从启动画面调用下一个 Activity 时,请尝试以下操作:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

try the following when calling the next Activity from your Splashscreen:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
浅暮の光 2024-11-22 22:48:36
<activity android:name=".SplashActivity" android:noHistory="true"/>

来自文档

是否应从活动堆栈中删除该活动
当用户离开时完成(调用其 finish() 方法)
从它开始,它在屏幕上不再可见 - 如果应该是“true”
完成,如果没有完成,则为“假”。默认值为“假”。

值为“true”意味着该活动不会留下历史记录
痕迹。它不会保留在任务的活动堆栈中,因此
用户将无法返回它。在这种情况下,
如果您为某个活动启动另一个活动,则永远不会调用 onActivityResult()
此活动的结果。

此属性是在 API 级别 3 中引入的。

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

From the documentation:

Whether or not the activity should be removed from the activity stack
and finished (its finish() method called) when the user navigates away
from it and it's no longer visible on screen — "true" if it should be
finished, and "false" if not. The default value is "false".

A value of "true" means that the activity will not leave a historical
trace. It will not remain in the activity stack for the task, so the
user will not be able to return to it. In this case,
onActivityResult() is never called if you start another activity for a
result from this activity.

This attribute was introduced in API Level 3.

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