How can I disable #39;go back#39; to some activity?(如何禁用“返回某些活动?)
问题描述
我不希望用户能够返回到我的应用的启动画面.一种解决方案似乎是检查当前活动下方的活动是否是启动画面的实例,在这种情况下退出应用程序,如下面的代码所示.但是,我不知道如何检查堆栈中的先前活动是什么.有人可以帮忙吗?有没有其他方法可以禁用返回"给定活动?
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);
}
}
推荐答案
在启动下一个活动后立即在启动画面活动中调用 finish().
Call finish() in your Splash Screen activity right after starting the next activity.
另一种方法是在 AndroidManifest.xml 中将此属性添加到您的活动中:android:noHistory="true"
Another approach is to add this attribute to your activity in AndroidManifest.xml: android:noHistory="true"
例子:
<activity android:name=".SplashActivity" android:noHistory="true"/>
此属性指示 Android 在其导航离开后从历史堆栈中删除 SplashActivity.
This attribute instructs Android to remove SplashActivity from the history stack once its navigated away from.
这篇关于如何禁用“返回"某些活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何禁用“返回"某些活动?
基础教程推荐
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
