Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable 'go back' 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);     } } 
like image 727
jul Avatar asked Jun 16 '11 18:06

jul


People also ask

How do I delete all back activity on Android?

Use finishAffinity(); in task C when starting task A to clear backstack activities. Show activity on this post. Use finishAffinity() to clear all backstack with existing one.

How do I go back to first activity on Android?

Declare A in your manifest with the android:launchMode="singleTask" . This way, when you call startActivity() from your other activies, and A is already running, it will just bring it to the front. Otherwise it'll launch a new instance.


1 Answers

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.

like image 82
Ryan Reeves Avatar answered Oct 05 '22 04:10

Ryan Reeves