如何禁用“返回”功能某些活动?
我不希望用户能够返回到我的应用程序的启动屏幕。一种解决方案似乎是检查当前活动下方的活动是否是闪屏的实例,在这种情况下退出应用程序,如下面的代码所示。但是,我不知道如何检查堆栈中的前一个活动是什么。有人可以帮忙吗?有没有其他方法可以禁用“返回”给定活动?
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
开始下一个活动后,立即在启动屏幕活动中调用
finish()
。另一种方法是将此属性添加到
AndroidManifest.xml
中的 Activity: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:
This attribute instructs Android to remove SplashActivity from the history stack once its navigated away from.
只需在
context.startActivity()
之后调用context.finish()
Just call
context.finish()
aftercontext.startActivity()
从启动画面调用下一个
Activity
时,请尝试以下操作:try the following when calling the next
Activity
from your Splashscreen:来自文档:
From the documentation: