Finish all activities at a time
Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish.
to do so apply the following code in ur project
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code finishes all the activities except for FirstActivity.
Then we need to finish the FirstActivity's
Enter the below code in Firstactivity's oncreate
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
and you are done....
how to finish all activities and close the application in android?
There is finishAffinity() method that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher.
Finish all previous activities
Use:
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities on top of home.
Assuming you are finishing the login screen when the user logs in and home is created and afterward all the screens from 1 to 5 on top of that one. The code I posted will return you to home screen finishing all the other activities. You can add an extra in the intent and read that in the home screen activity and finish it also (maybe launch login screen again from there or something).
I am not sure but you can also try going to login with this flag. I don't know how the activities will be ordered in that case. So don't know if it will clear the ones below the screen you are on including the one you are currently on but it's definitely the way to go.
Finish all previous activities from other activity
try this solution..
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
hope it helps.
how to finish all activities except the first activity?
To clear top activities from stack use below code
Intent intents = new Intent(A.this, B.class);
intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intents);
finish();
It will delete all activities from stack either asynctask run or not in the application.
It works fine and also a good approach
Android, Finish all activities
You should not implement an Exit button in an Android application.
Read http://groups.google.com/group/android-developers/browse_thread/thread/1bf0f7a4a9c62edd/a0a4aedf21ae5f76?pli=1
Cheers
How to finish Activity when starting other activity in Android?
You need to intent
your current context
to another activity first with startActivity
. After that you can finish
your current activity
from where you redirect.
Intent intent = new Intent(this, FirstActivity.class);// New activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
- Clears the activity stack. If you don't want to clear the activity stack. PLease don't use that flag then.
How to finish() two activities at the same time?
Two options:
- Call
finish()
in QuestionsActivity after you make the call to start the ResultsActivity. This will remove it from the stack so that pressing back from ResultsActivity returns to MainActivity. - Use
Intent.FLAG_ACTIVITY_CLEAR_TOP
in the intent to go back to MainActivity. This will clear all activities that are on top of it.
Related Topics
How to Add Apache Http API (Legacy) as Compile-Time Dependency to Build.Grade for Android M
Unable Instantiate Android.Gms.Maps.Mapfragment
Google Maps Android API V2 - Detect Touch on Map
Failed to Resolve: Com.Google.Firebase:Firebase-Core:9.0.0
How to Set Opacity (Alpha) for View in Android
Background Service with Location Listener in Android
How to Create Directory Automatically on Sd Card
How to Add a Button to Preferencescreen
Slowing Speed of Viewpager Controller in Android
Latest Update on Enabling and Disabling Mobile Data Programmatically
Android Studio 0.4 Duplicate Files Copied in APK Meta-Inf/License.Txt
How to Access the Drawable Resources by Name in Android
How to Dynamically Set the Position of View in Android
Android New Bottom Navigation Bar or Bottomnavigationview