- How to remove activity from backstack in android?
- 1 Answer 1
- Not the answer you’re looking for? Browse other questions tagged android or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Android:Delete a activity in backstack
- 6 Answers 6
- Android: Remove all the previous activities from the back stack
- 18 Answers 18
- Kotlin
- Removing an activity from the history stack
- 15 Answers 15
How to remove activity from backstack in android?
I have an app in which i have three activity namely:- Login,MainActivity and password activity.when I go to password activity and do some event then after login activity comes and here when i press back it will remove login and Mainactivity comes which I don’t want. what i want when i press device back twice it will simple close app not to come Mainactivity.How can I archeive this problem.
code that i have tried but not success.
and code for MainActivity
1 Answer 1
You need to clear the back-stack where you are using the Intent
Like this:
Here FirstActivity will get cleared from back-stack and will be finished. Also you will be navigated to SecondActivity . So when you press back button from SecondActivity , it will close the app.
Hope this will solve your problem.
Not the answer you’re looking for? Browse other questions tagged android or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Android:Delete a activity in backstack
I have got 4 activity let it be A->B->C->D.In every A,B,C activity user need to enter data all data will sent to server in C activity if the user data is correct he will move on to D activity and all the activity A,B,C removed from stack.If the data is in correct i need give the user to reenter data i.e is on back press it has to move C->B->A.My question is How to remove A,B,C activity when user enter D activity.
6 Answers 6
Use FLAG_ACTIVITY_CLEAR_TOP this shall solve your problem
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.
and also, take a look at this question:
Edit : I thought you want to move to your home activity from D and want to remove all activities from stack
Your stack would be like homeactivity , A , B , C , D so i gave you this solution as this shall remove all activities in stack on top of your home activity. But You want to cleat the the stack while going to D for that you can use
Источник
Android: Remove all the previous activities from the back stack
When i am clicking on Logout button in my Profile Activity i want to take user to Login page, where he needs to use new credentials.
Hence i used this code:
in the onButtonClick of the Logout button.
But the problem is when i click device back button on the Login Activity it takes me to the ProfileActivity. I was expecting the application should close when i press device back button on LoginActivity.
What am i doing wrong?
I also added android:launchMode=»singleTop» in the manifest for my LoginActivity
18 Answers 18
The solution proposed here worked for me:
Kotlin
However, it requires API level >= 11.
Here is one solution to clear all your application’s activities when you use the logout button.
Every time you start an Activity, start it like this:
When you want to close the entire app, do this:
RESULT_CLOSE_ALL is a final global variable with a unique integer to signal you want to close all activities.
Then define every activity’s onActivityResult(. ) callback so when an activity returns with the RESULT_CLOSE_ALL value, it also calls finish() :
This will cause a cascade effect that closes all your activities.
This is a hack however and uses startActivityForResult in a way that it was not designed to be used.
Perhaps a better way to do this would be using broadcast receivers as shown here:
See these threads for other methods as well:
Источник
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,
15 Answers 15
You can achieve this by setting the android:noHistory attribute to «true» in the relevant entries in your AndroidManifest.xml file. For example:
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() .
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:
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:
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.
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:
You also must add this to ActivitySplashScreen:
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:
Источник