38

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);
    }
}
jul
  • 36,404
  • 64
  • 191
  • 318

4 Answers4

123

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.

David d C e Freitas
  • 7,481
  • 4
  • 58
  • 67
Ryan Reeves
  • 10,209
  • 3
  • 42
  • 26
  • 1
    Thanks a lot "android:noHistory="true""- this was very helpfull! – Simcha Dec 09 '14 at 15:11
  • finish() (when added in original activity right after starting next activity) works because Android does not "go back" to an activity that is finished (destroyed), only to ones that are paused or stopped (alive). – Nerdy Bunz Jul 25 '16 at 01:06
  • using `android:noHistory="true"` will cause troubles since it doesnt clear all the tasks, @A. Binzxxxxxx 's answer is better – Ege Kuzubasioglu Feb 14 '18 at 08:57
21

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

LeffelMania
  • 12,765
  • 4
  • 32
  • 34
  • 5
    Good alternative, known as [forwarding](http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/Forwarding.html) – David Snabel-Caunt Jun 16 '11 at 19:18
  • Might not work for when you play a media file upon starting the next activity. As it might get cut short. – Mike6679 Nov 19 '13 at 05:43
9

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

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
A. Binzxxxxxx
  • 2,812
  • 1
  • 21
  • 33
2
<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.

David Miguel
  • 12,154
  • 3
  • 66
  • 68