如何禁用'回去'的一些活动?

2023-09-12 04:33:25 作者:辞忧.

我不希望用户能够回到我的应用程序的启动画面。一种解决方案似乎是,以检查是否低​​于当前一个活动是启动画面的实例,在此情况下退出该应用,如下面所述code。但是,我不知道如何检查什么在堆栈中的previous活动。任何人都可以帮忙吗?是否有禁用'回去'给定的活动?任何其他方式

  @覆盖
公共无效onBack pressed(){
    如果(小于在堆栈previous活性是启动画面的制造&gt实例;){
        意图exit_intent =新的意图(CurrentActivity.this,SplashScreen.class);
        exit_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        exit_intent.putExtra(EXIT,真正的);
        context.startActivity(exit_intent);
    }
}
 

解决方案

呼叫结束()开始下一个活动之后在闪屏的活动。

另一种方法是将该属性添加到您的活动的Andr​​oidManifest.xml 安卓noHistory =真正的

例如:

 <活动机器人:名称=机器人SplashActivity。:noHistory =真/>
 

这个属性指示的Andr​​oid从历史堆栈中删除SplashActivity一旦其从导航离开。

H M,请道歉

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

解决方案

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.

相关推荐