Removing an activity from the history stack
My app shows a signup activity the first time the user runs the app, looks like:
- ActivitySplashScreen (welcome to game, sign up for an account?)
- ActivitySplashScreenSignUp (great, fill in this info)
- ActivityGameMain (main game screen)
so the activities launch each other in exactly that order, when the user clicks through a button on each screen.
When the user goes from activity #2 to #3, is it possible to wipe #1 and #2 off the history stack completely? I'd like it so that if the user is at #3, and hits the back button, they just go to the homescreen, instead of back to the splash screen.
I think I can accomplish this with tasks (ie. start a new task on #3) but wanted to see if there was simpler method,
Thanks
Asked by: Daisy435 | Posted: 20-01-2022
Answer 1
You can achieve this by setting the android:noHistory
attribute to "true"
in the relevant <activity>
entries in your AndroidManifest.xml
file. For example:
<activity
android:name=".AnyActivity"
android:noHistory="true" />
Answered by: Roman416 | Posted: 21-02-2022
Answer 2
You can use forwarding to remove the previous activity from the activity stack while launching the next one. There's an example of this in the APIDemos, but basically all you're doing is calling finish()
immediately after calling startActivity()
.
Answer 3
Yes, have a look at Intent.FLAG_ACTIVITY_NO_HISTORY.
Answered by: Gianna967 | Posted: 21-02-2022Answer 4
This is likely not the ideal way to do it. If someone has a better way, I will be looking forward to implementing it. Here's how I accomplished this specific task with pre-version-11 sdk.
in each class you want to go away when it's clear time, you need to do this:
... interesting code stuff ...
Intent i = new Intent(MyActivityThatNeedsToGo.this, NextActivity.class);
startActivityForResult(i, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == R.string.unwind_stack_result_id) {
this.setResult(R.string.unwind_stack_result_id);
this.finish();
}
}
then the one that needs to set off the chain of pops from the stack needs to just call this when you want to initiate it:
NextActivity.this.setResult(R.string.unwind_stack_result_id);
NextActivity.this.finish();
Then the activities aren't on the stack!
Remember folks, that you can start an activity, and then begin cleaning up behind it, execution does not follow a single (the ui) thread.
Answer 5
One way that works pre API 11 is to start ActivityGameMain
first, then in the onCreate
of that Activity start your ActivitySplashScreen
activity. The ActivityGameMain
won't appear as you call startActivity too soon for the splash.
Then you can clear the stack when starting ActivityGameMain
by setting these flags on the Intent:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
You also must add this to ActivitySplashScreen:
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
So that pressing back on that activity doesn't go back to your ActivityGameMain
.
I assume you don't want the splash screen to be gone back to either, to achieve this I suggest setting it to noHistory
in your AndroidManifest.xml
. Then put the goBackPressed
code in your ActivitySplashScreenSignUp
class instead.
However I have found a few ways to break this. Start another app from a notification while ActivitySplashScreenSignUp
is shown and the back history is not reset.
The only real way around this is in API 11:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
Answered by: John987 | Posted: 21-02-2022
Answer 6
I use this way.
Intent i = new Intent(MyOldActivity.this, MyNewActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(i);
Answered by: Kellan778 | Posted: 21-02-2022
Answer 7
I know I'm late on this (it's been two years since the question was asked) but I accomplished this by intercepting the back button press. Rather than checking for specific activities, I just look at the count and if it's less than 3 it simply sends the app to the back (pausing the app and returning the user to whatever was running before launch). I check for less than three because I only have one intro screen. Also, I check the count because my app allows the user to navigate back to the home screen through the menu, so this allows them to back up through other screens like normal if there are activities other than the intro screen on the stack.
//We want the home screen to behave like the bottom of the activity stack so we do not return to the initial screen
//unless the application has been killed. Users can toggle the session mode with a menu item at all other times.
@Override
public void onBackPressed() {
//Check the activity stack and see if it's more than two deep (initial screen and home screen)
//If it's more than two deep, then let the app proccess the press
ActivityManager am = (ActivityManager)this.getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(3); //3 because we have to give it something. This is an arbitrary number
int activityCount = tasks.get(0).numActivities;
if (activityCount < 3)
{
moveTaskToBack(true);
}
else
{
super.onBackPressed();
}
}
Answered by: Edward953 | Posted: 21-02-2022
Answer 8
In the manifest you can add:
android:noHistory="true"
<activity
android:name=".ActivityName"
android:noHistory="true" />
You can also call
finish()
immediately after calling startActivity(..)
Answered by: Anna199 | Posted: 21-02-2022Answer 9
Just set noHistory="true"
in Manifest file.
It makes activity being removed from the backstack.
Answer 10
It is crazy that no one has mentioned this elegant solution. This should be the accepted answer.
SplashActivity -> AuthActivity -> DashActivity
if (!sessionManager.isLoggedIn()) {
Intent intent = new Intent(context, AuthActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
context.startActivity(intent);
finish();
} else {
Intent intent = new Intent(context, DashActivity.class);
context.startActivity(intent);
finish();
}
The key here is to use intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
for the intermediary Activity
. Once that middle link is broken, the DashActivity
will the first and last in the stack.
android:noHistory="true"
is a bad solution, as it causes problems when relying on the Activity
as a callback e.g onActivityResult
. This is the recommended solution and should be accepted.
Answer 11
It's too late but hope it helps. Most of the answers are not pointing into the right direction. There are two simple flags for such thing.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
From Android docs:
Answered by: Kimberly931 | Posted: 21-02-2022public static final int FLAG_ACTIVITY_CLEAR_TASK Added in API level 11
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the
activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
Answer 12
Just call this.finish() before startActivity(intent) like this-
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
this.finish();
startActivity(intent);
Answered by: Julia533 | Posted: 21-02-2022
Answer 13
Removing a activity from a History is done By setting the flag before the activity You Don't want
A->B->C->D
Suppose A,B,C and D are 4 Activities if you want to clear B and C then set flag
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
In the activity A and B
Here is the code bit
Intent intent = new Intent(this,Activity_B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Answered by: Roman896 | Posted: 21-02-2022
Answer 14
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
super.finishAndRemoveTask();
}
else {
super.finish();
}
Answered by: John364 | Posted: 21-02-2022
Answer 15
Here I have listed few ways to accomplish this task:
Go to the manifest.xml- and put android:noHistory="true", to remove the activity from the stack.
While switching from present activity to some other activity, in intent set flag as (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK). It is demonstrated in the example below.
Intent intent = new Intent(CurrentActivity.this, HomeActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) startActivity(intent);here
Note :Putting the intent flags can cause blank screen for sometime (while switching activity).
Answered by: Brianna289 | Posted: 21-02-2022Answer 16
Try this:
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)
it is API Level 1, check the link.
Answered by: Rubie891 | Posted: 21-02-2022Similar questions
android - Removing a View from an Activity
You've got a View defined in a layout file. How do you remove it from you Activity code?
Android - Removing a dialog themed activity
Android 2.1 via eclipse
I have an activity that opens a dialog themed activity via checkbox onChecked function
Im creating this new dialog themed activity with an Intent.
Problem is, how do i dismiss the dialog themed activity once i finish with it? (the way it stands now, i have to send a new intent in order to go back to the previous activity via click of a button)
Any help would be greatl...
android - Removing an activity from the stack
When going from activity A to B, I want to clear A off the stack: so when the user is pressing the back button in activity B, the app exits.
Intent intent = new Intent(A.this, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
These code lines do not work - the app goes back to activity A. I've also tried to OR with the flag Intent.FLAG_ACTIVITY_NEW_TAS...
android - Removing Activity From the Stack
I have 2 activities, normally I open activity A which then opens activity B.
In the case that a user clicks on a notification and the application is not running at all then B opens first. However when the users clicks the up/actionbackHome button the user should be taken back to A. I have done this by overwritting the up/actionbarHome button and starting activity A and calling finish on B. This works fine as if th...
android - Removing an Activity from History Stack
This question already has answers here:
android - Cordova removing my changes in main activity class after building cordova project
I am adding some extra stuff in my cordova project's Main activity class (overriding some functions like onResume) but cordova remove them after every cordovaBuild. What should I do to prevent this behaviuor?
android - Removing item from list on other activity
How can I remove a item from a ListActivity from a button on another Activity, the thing is,
I have this ListActivity:
public class ListaEventos extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onRestart();
repu...
android - Removing activity as a default launcher
I am trying to remove my activity as a default launcher. I followed this link but getting the error. Following is my code and error:
lockScreenAppActivity
@Override
protected void onResume() {
ComponentName componentName = new ComponentName(LockScreenAppActivity.this,LockSc...
Android - Removing Activity from Flavor
How to remove an activity from an app flavor? Here is a simplified example, I have an app which has the following two flavors (Paid and Free). The app is small and only has 3 activities (MainActivity, ActivityOne and ActivityTwo). The paid app does not need any limitations since it will use the full code base. The free app however requires it to have MainActivity and ActivityTwo accessible to the user and not ActivityOne. ...
xml - removing activity from Task manager stack in android
I am working on and Android application. When my application is running and i press the recent applications button on the phone , Activity's onPause() method is called . Inside this onPause i call finish() . So the activity gets destroyed.
But its still visible in the recent applications list.
How do i make sure it is not listed in the rec...
android - Removing a View from an Activity
You've got a View defined in a layout file. How do you remove it from you Activity code?
java - Removing the Window titlebar after adding content [Android]
The question is quite simple "How do I remove the titlebar from a WebView, after adding content ? Normally you use requestWindowFeature(Window.FEATURE_NO_TITLE); But you can only use that method before adding content :(
So any ideas ? :)
Thanks
Removing features from android OS
I want to customize the android OS for my specific needs.What i want to do exactly is eg:-
remove the access to android market, remove contacts, calendar.etc.
basically I want to have apps that I permit.
Even if removing the icon is all right.
android - options menu - removing focus from item
how do I remove focus from options menu item? I.e. when I open the menu for the first time, none of the items has focus. however, if I focus on one of them using track ball, and then close and re-open the menu the focus is still there. How do I get rid of it?
I am clearing and recreating the menu in onPrepareOptionsMenu (as I have to adjust it to the current activity state).
EDIT:
javascript - Removing address bar from browser (to view on Android)
Does anyone know how I can remove the address bar from the Android browser to better view my web app and make it look more like a native app?
Removing rows from an Android SQLite Cursor
I query and get a result set back, but I need to do some calculations that are impossible in the SQLite WHERE clause in order to determine what shows up in the ListView. How can I remove certain rows from the cursor? I know it is the same question as this Filter rows from Cursor so they don't show up in List...
Removing android phone app
I have source the code of android 2.1, and I want to remove phone app from it. But I am not able to remove it. At list first I want to remove it from launcher that it should not be visible in launcher but in manifest file of Phone app I can not able to find launcher category. I don't know what to do?
android - command for removing a specific over lay item
what is The command for removing specific overlay item ?
if here is my code of the added items
public void addOverLays(){
String [] coordinates = {"30.084262490272522","31.33625864982605" ,"30.084123015403748", "51.5002" , "-0.1262","31.337149143218994"};
double lat = 30.084262490272522, lat2 = 51.5002,lat3=29.987091422080994;
double log = 31.33625864982605, log2 = -0.1262,log3=31.43909454345703;
p = new G...
android - Removing space from Edit Text String
In my android app, I am getting the String from an Edit Text and using it as a parameter to call a web service and fetch JSON data.
Now, the method I use for getting the String value from Edit Text is like this :
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
Now normally it works fine, but if we the text in Edit Text contains spa...
layout - Android Dialog: Removing title bar
I have a weird behavior I can't pinpoint the source of.
I have my app with the classic
requestWindowFeature(Window.FEATURE_NO_TITLE);
to remove the title/status bar.
I then create a Dialog box to allow the user to enter information (name etc)
With a physical keyboard, no problem but when I use the virtual keyboard I have a strange behavior:
each time I hit a...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android