Clear the entire history stack and start a new activity on Android
Is it possible to start an activity on the stack, clearing the entire history before it?
The situation
I have an activity stack that either goes A->B->C or B->C (screen A selects the users token, but many users only have a single token).
In screen C the user may take an action which makes screen B invalid, so the application wants to take them to screen A, regardless of whether it is already in the stack. Screen A should then be the only item on the stack in my application.
Notes
There are many other similar questions, but I haven't found anything that answers this exact question. I tried calling getParent().finish()
- this always results in a null pointer exception. FLAG_ACTIVITY_CLEAR_TOP
only works if the activity is already on the stack.
android android-activity back-stack
add a comment |
Is it possible to start an activity on the stack, clearing the entire history before it?
The situation
I have an activity stack that either goes A->B->C or B->C (screen A selects the users token, but many users only have a single token).
In screen C the user may take an action which makes screen B invalid, so the application wants to take them to screen A, regardless of whether it is already in the stack. Screen A should then be the only item on the stack in my application.
Notes
There are many other similar questions, but I haven't found anything that answers this exact question. I tried calling getParent().finish()
- this always results in a null pointer exception. FLAG_ACTIVITY_CLEAR_TOP
only works if the activity is already on the stack.
android android-activity back-stack
add a comment |
Is it possible to start an activity on the stack, clearing the entire history before it?
The situation
I have an activity stack that either goes A->B->C or B->C (screen A selects the users token, but many users only have a single token).
In screen C the user may take an action which makes screen B invalid, so the application wants to take them to screen A, regardless of whether it is already in the stack. Screen A should then be the only item on the stack in my application.
Notes
There are many other similar questions, but I haven't found anything that answers this exact question. I tried calling getParent().finish()
- this always results in a null pointer exception. FLAG_ACTIVITY_CLEAR_TOP
only works if the activity is already on the stack.
android android-activity back-stack
Is it possible to start an activity on the stack, clearing the entire history before it?
The situation
I have an activity stack that either goes A->B->C or B->C (screen A selects the users token, but many users only have a single token).
In screen C the user may take an action which makes screen B invalid, so the application wants to take them to screen A, regardless of whether it is already in the stack. Screen A should then be the only item on the stack in my application.
Notes
There are many other similar questions, but I haven't found anything that answers this exact question. I tried calling getParent().finish()
- this always results in a null pointer exception. FLAG_ACTIVITY_CLEAR_TOP
only works if the activity is already on the stack.
android android-activity back-stack
android android-activity back-stack
edited Aug 14 '14 at 9:18
Marian Paździoch
3,77953765
3,77953765
asked Aug 13 '10 at 0:59
CasebashCasebash
48.7k71209318
48.7k71209318
add a comment |
add a comment |
11 Answers
11
active
oldest
votes
In API level 11 a new Intent Flag was added just for this: Intent.FLAG_ACTIVITY_CLEAR_TASK
Just to clarify, use this:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Unfortunately for API lvl <= 10, I haven't yet found a clean solution to this.
The "DontHackAndroidLikeThis" solution is indeed pure hackery. You should not do that. :)
Edit:
As per @Ben Pearson's comment, for API <=10 now one can use IntentCompat class for the same. One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
flag to clear task. So you can support pre API level 11 as well.
22
Just to clarify, use this: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
– user123321
Mar 12 '12 at 18:18
2
without the Intent.FLAG_ACTIVITY_NEW_TASK the app sometimes just closes itself on android 4
– max4ever
Oct 12 '12 at 15:00
20
IntentCompat has a flag to clear task now as well, so you can support pre API level 11 - developer.android.com/reference/android/support/v4/content/…
– Ben Pearson
Dec 12 '13 at 12:34
10
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK is ignored on devices with API level < 10. developer.android.com/reference/android/support/v4/content/…
– David
Jun 9 '14 at 20:11
7
IntentCompat's flag is only to avoid a crash, but doesn't do anything as @David says.
– Sloy
Aug 18 '14 at 8:44
|
show 9 more comments
Case 1:Only two activity A and B:
Here Activity flow is A->B .On clicking backbutton from B we need to close the application then while starting Activity B from A just call finish() this will prevent android from storing Activity A in to the Backstack.eg for activity A is Loding/Splash screen of application.
Intent newIntent = new Intent(A.this, B.class);
startActivity(newIntent);
finish();
Case 2:More than two activitiy:
If there is a flow like A->B->C->D->B and on clicking back button in Activity B while coming from Activity D.In that case we should use.
Intent newIntent = new Intent(D.this,B.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
Here Activity B will be started from the backstack rather than a new instance because of Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one.So when we press back button the whole application will be terminated.
2
This worked for me. I put in ALL activities those flags. In those activities back buttons work perfectly going to the previous activity, and in the main Activity with Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); The whole app is closed, still in memory but no active, and if u restart the app goes to the splash screen :)
– Rako
Jul 19 '13 at 10:50
add a comment |
With Android's Newer Version >= API 16 use finishAffinity()
approach is suitable for >= API 16.
Intent mIntent = new Intent(mContext,MainActivity.class);
finishAffinity();
startActivity(mIntent);
- Its is same as starting new Activity, and clear all stack.
- OR Restart to MainActivity/FirstActivity.
1
This did the trick, the flags werent working on 4.x.x for me and this worked perfectly! Thanks
– Jonathan Aste
Sep 23 '16 at 18:57
1
This seems to be the correct answer if your goal is to finish all activities below and including the current activity and start a new activity in their own task.
– ToBe
Apr 11 '18 at 12:25
add a comment |
I spent a few hours on this too ... and agree that FLAG_ACTIVITY_CLEAR_TOP sounds like what you'd want: clear the entire stack, except for the activity being launched, so the Back button exits the application. Yet as Mike Repass mentioned, FLAG_ACTIVITY_CLEAR_TOP only works when the activity you're launching is already in the stack; when the activity's not there, the flag doesn't do anything.
What to do? Put the activity being launching in the stack with FLAG_ACTIVITY_NEW_TASK, which makes that activity the start of a new task on the history stack. Then add the FLAG_ACTIVITY_CLEAR_TOP flag.
Now, when FLAG_ACTIVITY_CLEAR_TOP goes to find the new activity in the stack, it'll be there and be pulled up before everything else is cleared.
Here's my logout function; the View parameter is the button to which the function's attached.
public void onLogoutClick(final View view) {
Intent i = new Intent(this, Splash.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
}
1
do you mean CLEAR_TASK instead of CLEAR_TOP?
– Andy
Aug 19 '14 at 18:40
add a comment |
You shouldn't change the stack. Android back button should work as in a web browser.
I can think of a way to do it, but it's quite a hack.
Make your Activities
singleTask
by adding it to theAndroidManifest
Example:
<activity android:name=".activities.A"
android:label="@string/A_title"
android:launchMode="singleTask"/>
<activity android:name=".activities.B"
android:label="@string/B_title"
android:launchMode="singleTask"/>
Extend
Application
which will hold the logic of where to go.
Example:
public class DontHackAndroidLikeThis extends Application {
private Stack<Activity> classes = new Stack<Activity>();
public Activity getBackActivity() {
return classes.pop();
}
public void addBackActivity(Activity activity) {
classes.push(activity);
}
}
From A to B:
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.addBackActivity(A.class);
startActivity(this, B.class);
From B to C:
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.addBackActivity(B.class);
startActivity(this, C.class);
In C:
If ( shouldNotGoBackToB() ) {
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.pop();
}
and handle the back button to pop()
from the stack.
Once again, you shouldn't do this :)
In the end I decide to leave the Stack intact and just tell the user that their current screen was invalid
– Casebash
Aug 16 '10 at 1:42
1
Very frustrating that android doesn't let us manage the activity stack this way already. I would be tempted to use this solution in my future android apps.
– Cephron
Feb 19 '13 at 16:14
4
Just to be clear why this should not be used: it's a nice way to create memory leaks. At some point OS may decide to kill background activities, but sinceApplication
takes their instances the OS will not be able to free that RAM left from the destroyed activities.
– Vit Khudenko
Nov 12 '13 at 12:57
@Arhimed Are there other issues? The memory leak can be patched up by keeping only weak references.
– Navin
Dec 28 '14 at 13:33
1
@Navin yes, leaks can avoided with weak refs, but if after GC there will not be a live Activity ref then the entire approach is useless. Once again - don't do this, this is a wrong approach for Android.
– Vit Khudenko
Dec 29 '14 at 23:30
|
show 2 more comments
Immediately after you start a new activity, using startActivity
, make sure you call finish()
so that the current activity is not stacked behind the new one.
+1 Nice solution to prevent exactly one activity in a certain situation from beeing put onto the history stack.
– marsbear
Apr 27 '12 at 9:38
26
does not work if you have more than one activity in the stack the finish will just clear the previous activity but not the others....
– Necronet
Aug 13 '12 at 12:20
add a comment |
Try this:
Intent logout_intent = new Intent(DashboardActivity.this, LoginActivity.class);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(logout_intent);
finish();
add a comment |
Intent i = new Intent(MainPoliticalLogin.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
add a comment |
Try below code,
Intent intent = new Intent(ManageProfileActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_CLEAR_TASK|
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
if am using like this activityis updated once again call api but previously existing all statck is cleared
– Harsha
Nov 30 '18 at 11:03
add a comment |
I found too simple hack just do this add new element in AndroidManifest
as:-
<activity android:name=".activityName"
android:label="@string/app_name"
android:noHistory="true"/>
the android:noHistory
will clear your unwanted activity from Stack.
1
This aproachment can cause issues on Android 6.0+, if you ask for permitions in this Activity.
– Vitaliy A
Sep 3 '16 at 10:34
add a comment |
Sometimes your android emulator might fails to connect eclipse DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt.
1
Sometimes your android emulator might fails to connect eclipse DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt. Intent i = new Intent(OldActivity.this, NewActivity.class); // set the new task and clear flags i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) startActivity(i);
– RajeshkumarG
Oct 6 '16 at 9:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3473168%2fclear-the-entire-history-stack-and-start-a-new-activity-on-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
In API level 11 a new Intent Flag was added just for this: Intent.FLAG_ACTIVITY_CLEAR_TASK
Just to clarify, use this:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Unfortunately for API lvl <= 10, I haven't yet found a clean solution to this.
The "DontHackAndroidLikeThis" solution is indeed pure hackery. You should not do that. :)
Edit:
As per @Ben Pearson's comment, for API <=10 now one can use IntentCompat class for the same. One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
flag to clear task. So you can support pre API level 11 as well.
22
Just to clarify, use this: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
– user123321
Mar 12 '12 at 18:18
2
without the Intent.FLAG_ACTIVITY_NEW_TASK the app sometimes just closes itself on android 4
– max4ever
Oct 12 '12 at 15:00
20
IntentCompat has a flag to clear task now as well, so you can support pre API level 11 - developer.android.com/reference/android/support/v4/content/…
– Ben Pearson
Dec 12 '13 at 12:34
10
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK is ignored on devices with API level < 10. developer.android.com/reference/android/support/v4/content/…
– David
Jun 9 '14 at 20:11
7
IntentCompat's flag is only to avoid a crash, but doesn't do anything as @David says.
– Sloy
Aug 18 '14 at 8:44
|
show 9 more comments
In API level 11 a new Intent Flag was added just for this: Intent.FLAG_ACTIVITY_CLEAR_TASK
Just to clarify, use this:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Unfortunately for API lvl <= 10, I haven't yet found a clean solution to this.
The "DontHackAndroidLikeThis" solution is indeed pure hackery. You should not do that. :)
Edit:
As per @Ben Pearson's comment, for API <=10 now one can use IntentCompat class for the same. One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
flag to clear task. So you can support pre API level 11 as well.
22
Just to clarify, use this: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
– user123321
Mar 12 '12 at 18:18
2
without the Intent.FLAG_ACTIVITY_NEW_TASK the app sometimes just closes itself on android 4
– max4ever
Oct 12 '12 at 15:00
20
IntentCompat has a flag to clear task now as well, so you can support pre API level 11 - developer.android.com/reference/android/support/v4/content/…
– Ben Pearson
Dec 12 '13 at 12:34
10
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK is ignored on devices with API level < 10. developer.android.com/reference/android/support/v4/content/…
– David
Jun 9 '14 at 20:11
7
IntentCompat's flag is only to avoid a crash, but doesn't do anything as @David says.
– Sloy
Aug 18 '14 at 8:44
|
show 9 more comments
In API level 11 a new Intent Flag was added just for this: Intent.FLAG_ACTIVITY_CLEAR_TASK
Just to clarify, use this:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Unfortunately for API lvl <= 10, I haven't yet found a clean solution to this.
The "DontHackAndroidLikeThis" solution is indeed pure hackery. You should not do that. :)
Edit:
As per @Ben Pearson's comment, for API <=10 now one can use IntentCompat class for the same. One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
flag to clear task. So you can support pre API level 11 as well.
In API level 11 a new Intent Flag was added just for this: Intent.FLAG_ACTIVITY_CLEAR_TASK
Just to clarify, use this:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Unfortunately for API lvl <= 10, I haven't yet found a clean solution to this.
The "DontHackAndroidLikeThis" solution is indeed pure hackery. You should not do that. :)
Edit:
As per @Ben Pearson's comment, for API <=10 now one can use IntentCompat class for the same. One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
flag to clear task. So you can support pre API level 11 as well.
edited May 23 '17 at 12:02
Community♦
11
11
answered Mar 17 '11 at 20:10
Akos CzAkos Cz
10.8k13229
10.8k13229
22
Just to clarify, use this: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
– user123321
Mar 12 '12 at 18:18
2
without the Intent.FLAG_ACTIVITY_NEW_TASK the app sometimes just closes itself on android 4
– max4ever
Oct 12 '12 at 15:00
20
IntentCompat has a flag to clear task now as well, so you can support pre API level 11 - developer.android.com/reference/android/support/v4/content/…
– Ben Pearson
Dec 12 '13 at 12:34
10
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK is ignored on devices with API level < 10. developer.android.com/reference/android/support/v4/content/…
– David
Jun 9 '14 at 20:11
7
IntentCompat's flag is only to avoid a crash, but doesn't do anything as @David says.
– Sloy
Aug 18 '14 at 8:44
|
show 9 more comments
22
Just to clarify, use this: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
– user123321
Mar 12 '12 at 18:18
2
without the Intent.FLAG_ACTIVITY_NEW_TASK the app sometimes just closes itself on android 4
– max4ever
Oct 12 '12 at 15:00
20
IntentCompat has a flag to clear task now as well, so you can support pre API level 11 - developer.android.com/reference/android/support/v4/content/…
– Ben Pearson
Dec 12 '13 at 12:34
10
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK is ignored on devices with API level < 10. developer.android.com/reference/android/support/v4/content/…
– David
Jun 9 '14 at 20:11
7
IntentCompat's flag is only to avoid a crash, but doesn't do anything as @David says.
– Sloy
Aug 18 '14 at 8:44
22
22
Just to clarify, use this: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
– user123321
Mar 12 '12 at 18:18
Just to clarify, use this: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
– user123321
Mar 12 '12 at 18:18
2
2
without the Intent.FLAG_ACTIVITY_NEW_TASK the app sometimes just closes itself on android 4
– max4ever
Oct 12 '12 at 15:00
without the Intent.FLAG_ACTIVITY_NEW_TASK the app sometimes just closes itself on android 4
– max4ever
Oct 12 '12 at 15:00
20
20
IntentCompat has a flag to clear task now as well, so you can support pre API level 11 - developer.android.com/reference/android/support/v4/content/…
– Ben Pearson
Dec 12 '13 at 12:34
IntentCompat has a flag to clear task now as well, so you can support pre API level 11 - developer.android.com/reference/android/support/v4/content/…
– Ben Pearson
Dec 12 '13 at 12:34
10
10
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK is ignored on devices with API level < 10. developer.android.com/reference/android/support/v4/content/…
– David
Jun 9 '14 at 20:11
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK is ignored on devices with API level < 10. developer.android.com/reference/android/support/v4/content/…
– David
Jun 9 '14 at 20:11
7
7
IntentCompat's flag is only to avoid a crash, but doesn't do anything as @David says.
– Sloy
Aug 18 '14 at 8:44
IntentCompat's flag is only to avoid a crash, but doesn't do anything as @David says.
– Sloy
Aug 18 '14 at 8:44
|
show 9 more comments
Case 1:Only two activity A and B:
Here Activity flow is A->B .On clicking backbutton from B we need to close the application then while starting Activity B from A just call finish() this will prevent android from storing Activity A in to the Backstack.eg for activity A is Loding/Splash screen of application.
Intent newIntent = new Intent(A.this, B.class);
startActivity(newIntent);
finish();
Case 2:More than two activitiy:
If there is a flow like A->B->C->D->B and on clicking back button in Activity B while coming from Activity D.In that case we should use.
Intent newIntent = new Intent(D.this,B.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
Here Activity B will be started from the backstack rather than a new instance because of Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one.So when we press back button the whole application will be terminated.
2
This worked for me. I put in ALL activities those flags. In those activities back buttons work perfectly going to the previous activity, and in the main Activity with Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); The whole app is closed, still in memory but no active, and if u restart the app goes to the splash screen :)
– Rako
Jul 19 '13 at 10:50
add a comment |
Case 1:Only two activity A and B:
Here Activity flow is A->B .On clicking backbutton from B we need to close the application then while starting Activity B from A just call finish() this will prevent android from storing Activity A in to the Backstack.eg for activity A is Loding/Splash screen of application.
Intent newIntent = new Intent(A.this, B.class);
startActivity(newIntent);
finish();
Case 2:More than two activitiy:
If there is a flow like A->B->C->D->B and on clicking back button in Activity B while coming from Activity D.In that case we should use.
Intent newIntent = new Intent(D.this,B.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
Here Activity B will be started from the backstack rather than a new instance because of Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one.So when we press back button the whole application will be terminated.
2
This worked for me. I put in ALL activities those flags. In those activities back buttons work perfectly going to the previous activity, and in the main Activity with Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); The whole app is closed, still in memory but no active, and if u restart the app goes to the splash screen :)
– Rako
Jul 19 '13 at 10:50
add a comment |
Case 1:Only two activity A and B:
Here Activity flow is A->B .On clicking backbutton from B we need to close the application then while starting Activity B from A just call finish() this will prevent android from storing Activity A in to the Backstack.eg for activity A is Loding/Splash screen of application.
Intent newIntent = new Intent(A.this, B.class);
startActivity(newIntent);
finish();
Case 2:More than two activitiy:
If there is a flow like A->B->C->D->B and on clicking back button in Activity B while coming from Activity D.In that case we should use.
Intent newIntent = new Intent(D.this,B.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
Here Activity B will be started from the backstack rather than a new instance because of Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one.So when we press back button the whole application will be terminated.
Case 1:Only two activity A and B:
Here Activity flow is A->B .On clicking backbutton from B we need to close the application then while starting Activity B from A just call finish() this will prevent android from storing Activity A in to the Backstack.eg for activity A is Loding/Splash screen of application.
Intent newIntent = new Intent(A.this, B.class);
startActivity(newIntent);
finish();
Case 2:More than two activitiy:
If there is a flow like A->B->C->D->B and on clicking back button in Activity B while coming from Activity D.In that case we should use.
Intent newIntent = new Intent(D.this,B.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
Here Activity B will be started from the backstack rather than a new instance because of Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one.So when we press back button the whole application will be terminated.
answered May 3 '13 at 12:45
monish georgemonish george
568513
568513
2
This worked for me. I put in ALL activities those flags. In those activities back buttons work perfectly going to the previous activity, and in the main Activity with Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); The whole app is closed, still in memory but no active, and if u restart the app goes to the splash screen :)
– Rako
Jul 19 '13 at 10:50
add a comment |
2
This worked for me. I put in ALL activities those flags. In those activities back buttons work perfectly going to the previous activity, and in the main Activity with Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); The whole app is closed, still in memory but no active, and if u restart the app goes to the splash screen :)
– Rako
Jul 19 '13 at 10:50
2
2
This worked for me. I put in ALL activities those flags. In those activities back buttons work perfectly going to the previous activity, and in the main Activity with Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); The whole app is closed, still in memory but no active, and if u restart the app goes to the splash screen :)
– Rako
Jul 19 '13 at 10:50
This worked for me. I put in ALL activities those flags. In those activities back buttons work perfectly going to the previous activity, and in the main Activity with Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); The whole app is closed, still in memory but no active, and if u restart the app goes to the splash screen :)
– Rako
Jul 19 '13 at 10:50
add a comment |
With Android's Newer Version >= API 16 use finishAffinity()
approach is suitable for >= API 16.
Intent mIntent = new Intent(mContext,MainActivity.class);
finishAffinity();
startActivity(mIntent);
- Its is same as starting new Activity, and clear all stack.
- OR Restart to MainActivity/FirstActivity.
1
This did the trick, the flags werent working on 4.x.x for me and this worked perfectly! Thanks
– Jonathan Aste
Sep 23 '16 at 18:57
1
This seems to be the correct answer if your goal is to finish all activities below and including the current activity and start a new activity in their own task.
– ToBe
Apr 11 '18 at 12:25
add a comment |
With Android's Newer Version >= API 16 use finishAffinity()
approach is suitable for >= API 16.
Intent mIntent = new Intent(mContext,MainActivity.class);
finishAffinity();
startActivity(mIntent);
- Its is same as starting new Activity, and clear all stack.
- OR Restart to MainActivity/FirstActivity.
1
This did the trick, the flags werent working on 4.x.x for me and this worked perfectly! Thanks
– Jonathan Aste
Sep 23 '16 at 18:57
1
This seems to be the correct answer if your goal is to finish all activities below and including the current activity and start a new activity in their own task.
– ToBe
Apr 11 '18 at 12:25
add a comment |
With Android's Newer Version >= API 16 use finishAffinity()
approach is suitable for >= API 16.
Intent mIntent = new Intent(mContext,MainActivity.class);
finishAffinity();
startActivity(mIntent);
- Its is same as starting new Activity, and clear all stack.
- OR Restart to MainActivity/FirstActivity.
With Android's Newer Version >= API 16 use finishAffinity()
approach is suitable for >= API 16.
Intent mIntent = new Intent(mContext,MainActivity.class);
finishAffinity();
startActivity(mIntent);
- Its is same as starting new Activity, and clear all stack.
- OR Restart to MainActivity/FirstActivity.
answered Sep 14 '16 at 7:19
karankaran
2,50512940
2,50512940
1
This did the trick, the flags werent working on 4.x.x for me and this worked perfectly! Thanks
– Jonathan Aste
Sep 23 '16 at 18:57
1
This seems to be the correct answer if your goal is to finish all activities below and including the current activity and start a new activity in their own task.
– ToBe
Apr 11 '18 at 12:25
add a comment |
1
This did the trick, the flags werent working on 4.x.x for me and this worked perfectly! Thanks
– Jonathan Aste
Sep 23 '16 at 18:57
1
This seems to be the correct answer if your goal is to finish all activities below and including the current activity and start a new activity in their own task.
– ToBe
Apr 11 '18 at 12:25
1
1
This did the trick, the flags werent working on 4.x.x for me and this worked perfectly! Thanks
– Jonathan Aste
Sep 23 '16 at 18:57
This did the trick, the flags werent working on 4.x.x for me and this worked perfectly! Thanks
– Jonathan Aste
Sep 23 '16 at 18:57
1
1
This seems to be the correct answer if your goal is to finish all activities below and including the current activity and start a new activity in their own task.
– ToBe
Apr 11 '18 at 12:25
This seems to be the correct answer if your goal is to finish all activities below and including the current activity and start a new activity in their own task.
– ToBe
Apr 11 '18 at 12:25
add a comment |
I spent a few hours on this too ... and agree that FLAG_ACTIVITY_CLEAR_TOP sounds like what you'd want: clear the entire stack, except for the activity being launched, so the Back button exits the application. Yet as Mike Repass mentioned, FLAG_ACTIVITY_CLEAR_TOP only works when the activity you're launching is already in the stack; when the activity's not there, the flag doesn't do anything.
What to do? Put the activity being launching in the stack with FLAG_ACTIVITY_NEW_TASK, which makes that activity the start of a new task on the history stack. Then add the FLAG_ACTIVITY_CLEAR_TOP flag.
Now, when FLAG_ACTIVITY_CLEAR_TOP goes to find the new activity in the stack, it'll be there and be pulled up before everything else is cleared.
Here's my logout function; the View parameter is the button to which the function's attached.
public void onLogoutClick(final View view) {
Intent i = new Intent(this, Splash.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
}
1
do you mean CLEAR_TASK instead of CLEAR_TOP?
– Andy
Aug 19 '14 at 18:40
add a comment |
I spent a few hours on this too ... and agree that FLAG_ACTIVITY_CLEAR_TOP sounds like what you'd want: clear the entire stack, except for the activity being launched, so the Back button exits the application. Yet as Mike Repass mentioned, FLAG_ACTIVITY_CLEAR_TOP only works when the activity you're launching is already in the stack; when the activity's not there, the flag doesn't do anything.
What to do? Put the activity being launching in the stack with FLAG_ACTIVITY_NEW_TASK, which makes that activity the start of a new task on the history stack. Then add the FLAG_ACTIVITY_CLEAR_TOP flag.
Now, when FLAG_ACTIVITY_CLEAR_TOP goes to find the new activity in the stack, it'll be there and be pulled up before everything else is cleared.
Here's my logout function; the View parameter is the button to which the function's attached.
public void onLogoutClick(final View view) {
Intent i = new Intent(this, Splash.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
}
1
do you mean CLEAR_TASK instead of CLEAR_TOP?
– Andy
Aug 19 '14 at 18:40
add a comment |
I spent a few hours on this too ... and agree that FLAG_ACTIVITY_CLEAR_TOP sounds like what you'd want: clear the entire stack, except for the activity being launched, so the Back button exits the application. Yet as Mike Repass mentioned, FLAG_ACTIVITY_CLEAR_TOP only works when the activity you're launching is already in the stack; when the activity's not there, the flag doesn't do anything.
What to do? Put the activity being launching in the stack with FLAG_ACTIVITY_NEW_TASK, which makes that activity the start of a new task on the history stack. Then add the FLAG_ACTIVITY_CLEAR_TOP flag.
Now, when FLAG_ACTIVITY_CLEAR_TOP goes to find the new activity in the stack, it'll be there and be pulled up before everything else is cleared.
Here's my logout function; the View parameter is the button to which the function's attached.
public void onLogoutClick(final View view) {
Intent i = new Intent(this, Splash.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
}
I spent a few hours on this too ... and agree that FLAG_ACTIVITY_CLEAR_TOP sounds like what you'd want: clear the entire stack, except for the activity being launched, so the Back button exits the application. Yet as Mike Repass mentioned, FLAG_ACTIVITY_CLEAR_TOP only works when the activity you're launching is already in the stack; when the activity's not there, the flag doesn't do anything.
What to do? Put the activity being launching in the stack with FLAG_ACTIVITY_NEW_TASK, which makes that activity the start of a new task on the history stack. Then add the FLAG_ACTIVITY_CLEAR_TOP flag.
Now, when FLAG_ACTIVITY_CLEAR_TOP goes to find the new activity in the stack, it'll be there and be pulled up before everything else is cleared.
Here's my logout function; the View parameter is the button to which the function's attached.
public void onLogoutClick(final View view) {
Intent i = new Intent(this, Splash.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
}
answered Oct 18 '13 at 15:29
user2895402user2895402
19112
19112
1
do you mean CLEAR_TASK instead of CLEAR_TOP?
– Andy
Aug 19 '14 at 18:40
add a comment |
1
do you mean CLEAR_TASK instead of CLEAR_TOP?
– Andy
Aug 19 '14 at 18:40
1
1
do you mean CLEAR_TASK instead of CLEAR_TOP?
– Andy
Aug 19 '14 at 18:40
do you mean CLEAR_TASK instead of CLEAR_TOP?
– Andy
Aug 19 '14 at 18:40
add a comment |
You shouldn't change the stack. Android back button should work as in a web browser.
I can think of a way to do it, but it's quite a hack.
Make your Activities
singleTask
by adding it to theAndroidManifest
Example:
<activity android:name=".activities.A"
android:label="@string/A_title"
android:launchMode="singleTask"/>
<activity android:name=".activities.B"
android:label="@string/B_title"
android:launchMode="singleTask"/>
Extend
Application
which will hold the logic of where to go.
Example:
public class DontHackAndroidLikeThis extends Application {
private Stack<Activity> classes = new Stack<Activity>();
public Activity getBackActivity() {
return classes.pop();
}
public void addBackActivity(Activity activity) {
classes.push(activity);
}
}
From A to B:
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.addBackActivity(A.class);
startActivity(this, B.class);
From B to C:
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.addBackActivity(B.class);
startActivity(this, C.class);
In C:
If ( shouldNotGoBackToB() ) {
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.pop();
}
and handle the back button to pop()
from the stack.
Once again, you shouldn't do this :)
In the end I decide to leave the Stack intact and just tell the user that their current screen was invalid
– Casebash
Aug 16 '10 at 1:42
1
Very frustrating that android doesn't let us manage the activity stack this way already. I would be tempted to use this solution in my future android apps.
– Cephron
Feb 19 '13 at 16:14
4
Just to be clear why this should not be used: it's a nice way to create memory leaks. At some point OS may decide to kill background activities, but sinceApplication
takes their instances the OS will not be able to free that RAM left from the destroyed activities.
– Vit Khudenko
Nov 12 '13 at 12:57
@Arhimed Are there other issues? The memory leak can be patched up by keeping only weak references.
– Navin
Dec 28 '14 at 13:33
1
@Navin yes, leaks can avoided with weak refs, but if after GC there will not be a live Activity ref then the entire approach is useless. Once again - don't do this, this is a wrong approach for Android.
– Vit Khudenko
Dec 29 '14 at 23:30
|
show 2 more comments
You shouldn't change the stack. Android back button should work as in a web browser.
I can think of a way to do it, but it's quite a hack.
Make your Activities
singleTask
by adding it to theAndroidManifest
Example:
<activity android:name=".activities.A"
android:label="@string/A_title"
android:launchMode="singleTask"/>
<activity android:name=".activities.B"
android:label="@string/B_title"
android:launchMode="singleTask"/>
Extend
Application
which will hold the logic of where to go.
Example:
public class DontHackAndroidLikeThis extends Application {
private Stack<Activity> classes = new Stack<Activity>();
public Activity getBackActivity() {
return classes.pop();
}
public void addBackActivity(Activity activity) {
classes.push(activity);
}
}
From A to B:
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.addBackActivity(A.class);
startActivity(this, B.class);
From B to C:
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.addBackActivity(B.class);
startActivity(this, C.class);
In C:
If ( shouldNotGoBackToB() ) {
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.pop();
}
and handle the back button to pop()
from the stack.
Once again, you shouldn't do this :)
In the end I decide to leave the Stack intact and just tell the user that their current screen was invalid
– Casebash
Aug 16 '10 at 1:42
1
Very frustrating that android doesn't let us manage the activity stack this way already. I would be tempted to use this solution in my future android apps.
– Cephron
Feb 19 '13 at 16:14
4
Just to be clear why this should not be used: it's a nice way to create memory leaks. At some point OS may decide to kill background activities, but sinceApplication
takes their instances the OS will not be able to free that RAM left from the destroyed activities.
– Vit Khudenko
Nov 12 '13 at 12:57
@Arhimed Are there other issues? The memory leak can be patched up by keeping only weak references.
– Navin
Dec 28 '14 at 13:33
1
@Navin yes, leaks can avoided with weak refs, but if after GC there will not be a live Activity ref then the entire approach is useless. Once again - don't do this, this is a wrong approach for Android.
– Vit Khudenko
Dec 29 '14 at 23:30
|
show 2 more comments
You shouldn't change the stack. Android back button should work as in a web browser.
I can think of a way to do it, but it's quite a hack.
Make your Activities
singleTask
by adding it to theAndroidManifest
Example:
<activity android:name=".activities.A"
android:label="@string/A_title"
android:launchMode="singleTask"/>
<activity android:name=".activities.B"
android:label="@string/B_title"
android:launchMode="singleTask"/>
Extend
Application
which will hold the logic of where to go.
Example:
public class DontHackAndroidLikeThis extends Application {
private Stack<Activity> classes = new Stack<Activity>();
public Activity getBackActivity() {
return classes.pop();
}
public void addBackActivity(Activity activity) {
classes.push(activity);
}
}
From A to B:
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.addBackActivity(A.class);
startActivity(this, B.class);
From B to C:
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.addBackActivity(B.class);
startActivity(this, C.class);
In C:
If ( shouldNotGoBackToB() ) {
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.pop();
}
and handle the back button to pop()
from the stack.
Once again, you shouldn't do this :)
You shouldn't change the stack. Android back button should work as in a web browser.
I can think of a way to do it, but it's quite a hack.
Make your Activities
singleTask
by adding it to theAndroidManifest
Example:
<activity android:name=".activities.A"
android:label="@string/A_title"
android:launchMode="singleTask"/>
<activity android:name=".activities.B"
android:label="@string/B_title"
android:launchMode="singleTask"/>
Extend
Application
which will hold the logic of where to go.
Example:
public class DontHackAndroidLikeThis extends Application {
private Stack<Activity> classes = new Stack<Activity>();
public Activity getBackActivity() {
return classes.pop();
}
public void addBackActivity(Activity activity) {
classes.push(activity);
}
}
From A to B:
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.addBackActivity(A.class);
startActivity(this, B.class);
From B to C:
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.addBackActivity(B.class);
startActivity(this, C.class);
In C:
If ( shouldNotGoBackToB() ) {
DontHackAndroidLikeThis app = (DontHackAndroidLikeThis) getApplication();
app.pop();
}
and handle the back button to pop()
from the stack.
Once again, you shouldn't do this :)
edited Oct 27 '12 at 15:28
Shankar Agarwal
34k86063
34k86063
answered Aug 13 '10 at 1:59
MacarseMacarse
61.8k40156221
61.8k40156221
In the end I decide to leave the Stack intact and just tell the user that their current screen was invalid
– Casebash
Aug 16 '10 at 1:42
1
Very frustrating that android doesn't let us manage the activity stack this way already. I would be tempted to use this solution in my future android apps.
– Cephron
Feb 19 '13 at 16:14
4
Just to be clear why this should not be used: it's a nice way to create memory leaks. At some point OS may decide to kill background activities, but sinceApplication
takes their instances the OS will not be able to free that RAM left from the destroyed activities.
– Vit Khudenko
Nov 12 '13 at 12:57
@Arhimed Are there other issues? The memory leak can be patched up by keeping only weak references.
– Navin
Dec 28 '14 at 13:33
1
@Navin yes, leaks can avoided with weak refs, but if after GC there will not be a live Activity ref then the entire approach is useless. Once again - don't do this, this is a wrong approach for Android.
– Vit Khudenko
Dec 29 '14 at 23:30
|
show 2 more comments
In the end I decide to leave the Stack intact and just tell the user that their current screen was invalid
– Casebash
Aug 16 '10 at 1:42
1
Very frustrating that android doesn't let us manage the activity stack this way already. I would be tempted to use this solution in my future android apps.
– Cephron
Feb 19 '13 at 16:14
4
Just to be clear why this should not be used: it's a nice way to create memory leaks. At some point OS may decide to kill background activities, but sinceApplication
takes their instances the OS will not be able to free that RAM left from the destroyed activities.
– Vit Khudenko
Nov 12 '13 at 12:57
@Arhimed Are there other issues? The memory leak can be patched up by keeping only weak references.
– Navin
Dec 28 '14 at 13:33
1
@Navin yes, leaks can avoided with weak refs, but if after GC there will not be a live Activity ref then the entire approach is useless. Once again - don't do this, this is a wrong approach for Android.
– Vit Khudenko
Dec 29 '14 at 23:30
In the end I decide to leave the Stack intact and just tell the user that their current screen was invalid
– Casebash
Aug 16 '10 at 1:42
In the end I decide to leave the Stack intact and just tell the user that their current screen was invalid
– Casebash
Aug 16 '10 at 1:42
1
1
Very frustrating that android doesn't let us manage the activity stack this way already. I would be tempted to use this solution in my future android apps.
– Cephron
Feb 19 '13 at 16:14
Very frustrating that android doesn't let us manage the activity stack this way already. I would be tempted to use this solution in my future android apps.
– Cephron
Feb 19 '13 at 16:14
4
4
Just to be clear why this should not be used: it's a nice way to create memory leaks. At some point OS may decide to kill background activities, but since
Application
takes their instances the OS will not be able to free that RAM left from the destroyed activities.– Vit Khudenko
Nov 12 '13 at 12:57
Just to be clear why this should not be used: it's a nice way to create memory leaks. At some point OS may decide to kill background activities, but since
Application
takes their instances the OS will not be able to free that RAM left from the destroyed activities.– Vit Khudenko
Nov 12 '13 at 12:57
@Arhimed Are there other issues? The memory leak can be patched up by keeping only weak references.
– Navin
Dec 28 '14 at 13:33
@Arhimed Are there other issues? The memory leak can be patched up by keeping only weak references.
– Navin
Dec 28 '14 at 13:33
1
1
@Navin yes, leaks can avoided with weak refs, but if after GC there will not be a live Activity ref then the entire approach is useless. Once again - don't do this, this is a wrong approach for Android.
– Vit Khudenko
Dec 29 '14 at 23:30
@Navin yes, leaks can avoided with weak refs, but if after GC there will not be a live Activity ref then the entire approach is useless. Once again - don't do this, this is a wrong approach for Android.
– Vit Khudenko
Dec 29 '14 at 23:30
|
show 2 more comments
Immediately after you start a new activity, using startActivity
, make sure you call finish()
so that the current activity is not stacked behind the new one.
+1 Nice solution to prevent exactly one activity in a certain situation from beeing put onto the history stack.
– marsbear
Apr 27 '12 at 9:38
26
does not work if you have more than one activity in the stack the finish will just clear the previous activity but not the others....
– Necronet
Aug 13 '12 at 12:20
add a comment |
Immediately after you start a new activity, using startActivity
, make sure you call finish()
so that the current activity is not stacked behind the new one.
+1 Nice solution to prevent exactly one activity in a certain situation from beeing put onto the history stack.
– marsbear
Apr 27 '12 at 9:38
26
does not work if you have more than one activity in the stack the finish will just clear the previous activity but not the others....
– Necronet
Aug 13 '12 at 12:20
add a comment |
Immediately after you start a new activity, using startActivity
, make sure you call finish()
so that the current activity is not stacked behind the new one.
Immediately after you start a new activity, using startActivity
, make sure you call finish()
so that the current activity is not stacked behind the new one.
answered Apr 4 '12 at 16:44
Keith MaurinoKeith Maurino
1,919103146
1,919103146
+1 Nice solution to prevent exactly one activity in a certain situation from beeing put onto the history stack.
– marsbear
Apr 27 '12 at 9:38
26
does not work if you have more than one activity in the stack the finish will just clear the previous activity but not the others....
– Necronet
Aug 13 '12 at 12:20
add a comment |
+1 Nice solution to prevent exactly one activity in a certain situation from beeing put onto the history stack.
– marsbear
Apr 27 '12 at 9:38
26
does not work if you have more than one activity in the stack the finish will just clear the previous activity but not the others....
– Necronet
Aug 13 '12 at 12:20
+1 Nice solution to prevent exactly one activity in a certain situation from beeing put onto the history stack.
– marsbear
Apr 27 '12 at 9:38
+1 Nice solution to prevent exactly one activity in a certain situation from beeing put onto the history stack.
– marsbear
Apr 27 '12 at 9:38
26
26
does not work if you have more than one activity in the stack the finish will just clear the previous activity but not the others....
– Necronet
Aug 13 '12 at 12:20
does not work if you have more than one activity in the stack the finish will just clear the previous activity but not the others....
– Necronet
Aug 13 '12 at 12:20
add a comment |
Try this:
Intent logout_intent = new Intent(DashboardActivity.this, LoginActivity.class);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(logout_intent);
finish();
add a comment |
Try this:
Intent logout_intent = new Intent(DashboardActivity.this, LoginActivity.class);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(logout_intent);
finish();
add a comment |
Try this:
Intent logout_intent = new Intent(DashboardActivity.this, LoginActivity.class);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(logout_intent);
finish();
Try this:
Intent logout_intent = new Intent(DashboardActivity.this, LoginActivity.class);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(logout_intent);
finish();
edited Jun 13 '17 at 4:48
Pang
6,8991664102
6,8991664102
answered Jun 13 '17 at 4:29
MohammadMohammad
19410
19410
add a comment |
add a comment |
Intent i = new Intent(MainPoliticalLogin.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
add a comment |
Intent i = new Intent(MainPoliticalLogin.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
add a comment |
Intent i = new Intent(MainPoliticalLogin.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
Intent i = new Intent(MainPoliticalLogin.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
edited Jul 4 '18 at 9:27
Joaquin Iurchuk
4,11813155
4,11813155
answered Dec 13 '17 at 11:56
Neeraj GuptaNeeraj Gupta
312
312
add a comment |
add a comment |
Try below code,
Intent intent = new Intent(ManageProfileActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_CLEAR_TASK|
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
if am using like this activityis updated once again call api but previously existing all statck is cleared
– Harsha
Nov 30 '18 at 11:03
add a comment |
Try below code,
Intent intent = new Intent(ManageProfileActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_CLEAR_TASK|
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
if am using like this activityis updated once again call api but previously existing all statck is cleared
– Harsha
Nov 30 '18 at 11:03
add a comment |
Try below code,
Intent intent = new Intent(ManageProfileActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_CLEAR_TASK|
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Try below code,
Intent intent = new Intent(ManageProfileActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_CLEAR_TASK|
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
edited Nov 14 '18 at 10:34
Sagar Zala
2,37441336
2,37441336
answered Nov 14 '18 at 8:39
shashikant yadavshashikant yadav
1
1
if am using like this activityis updated once again call api but previously existing all statck is cleared
– Harsha
Nov 30 '18 at 11:03
add a comment |
if am using like this activityis updated once again call api but previously existing all statck is cleared
– Harsha
Nov 30 '18 at 11:03
if am using like this activityis updated once again call api but previously existing all statck is cleared
– Harsha
Nov 30 '18 at 11:03
if am using like this activityis updated once again call api but previously existing all statck is cleared
– Harsha
Nov 30 '18 at 11:03
add a comment |
I found too simple hack just do this add new element in AndroidManifest
as:-
<activity android:name=".activityName"
android:label="@string/app_name"
android:noHistory="true"/>
the android:noHistory
will clear your unwanted activity from Stack.
1
This aproachment can cause issues on Android 6.0+, if you ask for permitions in this Activity.
– Vitaliy A
Sep 3 '16 at 10:34
add a comment |
I found too simple hack just do this add new element in AndroidManifest
as:-
<activity android:name=".activityName"
android:label="@string/app_name"
android:noHistory="true"/>
the android:noHistory
will clear your unwanted activity from Stack.
1
This aproachment can cause issues on Android 6.0+, if you ask for permitions in this Activity.
– Vitaliy A
Sep 3 '16 at 10:34
add a comment |
I found too simple hack just do this add new element in AndroidManifest
as:-
<activity android:name=".activityName"
android:label="@string/app_name"
android:noHistory="true"/>
the android:noHistory
will clear your unwanted activity from Stack.
I found too simple hack just do this add new element in AndroidManifest
as:-
<activity android:name=".activityName"
android:label="@string/app_name"
android:noHistory="true"/>
the android:noHistory
will clear your unwanted activity from Stack.
answered Jul 19 '16 at 8:43
TauseefTauseef
8417
8417
1
This aproachment can cause issues on Android 6.0+, if you ask for permitions in this Activity.
– Vitaliy A
Sep 3 '16 at 10:34
add a comment |
1
This aproachment can cause issues on Android 6.0+, if you ask for permitions in this Activity.
– Vitaliy A
Sep 3 '16 at 10:34
1
1
This aproachment can cause issues on Android 6.0+, if you ask for permitions in this Activity.
– Vitaliy A
Sep 3 '16 at 10:34
This aproachment can cause issues on Android 6.0+, if you ask for permitions in this Activity.
– Vitaliy A
Sep 3 '16 at 10:34
add a comment |
Sometimes your android emulator might fails to connect eclipse DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt.
1
Sometimes your android emulator might fails to connect eclipse DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt. Intent i = new Intent(OldActivity.this, NewActivity.class); // set the new task and clear flags i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) startActivity(i);
– RajeshkumarG
Oct 6 '16 at 9:11
add a comment |
Sometimes your android emulator might fails to connect eclipse DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt.
1
Sometimes your android emulator might fails to connect eclipse DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt. Intent i = new Intent(OldActivity.this, NewActivity.class); // set the new task and clear flags i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) startActivity(i);
– RajeshkumarG
Oct 6 '16 at 9:11
add a comment |
Sometimes your android emulator might fails to connect eclipse DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt.
Sometimes your android emulator might fails to connect eclipse DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt.
answered Oct 6 '16 at 9:08
RajeshkumarGRajeshkumarG
114
114
1
Sometimes your android emulator might fails to connect eclipse DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt. Intent i = new Intent(OldActivity.this, NewActivity.class); // set the new task and clear flags i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) startActivity(i);
– RajeshkumarG
Oct 6 '16 at 9:11
add a comment |
1
Sometimes your android emulator might fails to connect eclipse DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt. Intent i = new Intent(OldActivity.this, NewActivity.class); // set the new task and clear flags i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) startActivity(i);
– RajeshkumarG
Oct 6 '16 at 9:11
1
1
Sometimes your android emulator might fails to connect eclipse DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt. Intent i = new Intent(OldActivity.this, NewActivity.class); // set the new task and clear flags i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) startActivity(i);
– RajeshkumarG
Oct 6 '16 at 9:11
Sometimes your android emulator might fails to connect eclipse DDMS tool and ask for adb to start manually. In that case you can start or stop the adb using the command prompt. Intent i = new Intent(OldActivity.this, NewActivity.class); // set the new task and clear flags i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) startActivity(i);
– RajeshkumarG
Oct 6 '16 at 9:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3473168%2fclear-the-entire-history-stack-and-start-a-new-activity-on-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown