My application has the following flow:
Home->screen 1->screen 2->screen 3->screen 4->screen 5>Home->screen 2->Home->Screen 3
My problem is that when I am trying to close the application then Home activity opens everytime when I am trying to close the application.
I just want to close the application when user presses the back key of device on home screen.
There is finishAffinity() method that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher.
This works well for me.
You should using
FLAG_ACTIVITY_CLEAR_TASK
andFLAG_ACTIVITY_NEW_TASK
flags.Intent intent = new Intent(SecondActivity.this, CloseActivity.class); //Clear all activities and start new task intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
onCreate()
method ofCloseActivity
activity.@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); finish(); // Exit }
To clear all the activities while opening new one then do the following:
Intent intent = new Intent(getApplicationContext(), YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This works well for me in all versions. Close all the previous activities as follows:
Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // this will clear all the stack
intent.putExtra("Exit me", true);
startActivity(intent);
finish();
Then in HomeActivity onCreate() method add this to finish the MainActivity
setContentView(R.layout.main_layout);
if( getIntent().getBooleanExtra("Exit me", false)){
finish();
return; // add this to prevent from doing unnecessary stuffs
}
Sometime finish()
not working
I have solved that issue with
finishAffinity()
Do not use
System.exit(0);
It will finish app without annimation.
Use finishAffinity()
method that will finish the current activity and all parent activities. But it works only for API 16+
mean Android 4.1 or higher.
API 16+ use:
finishAffinity();
Below API 16 use:
ActivityCompat.finishAffinity(this); //with v4 support library
To exit whole app:
finishAffinity(); // Close all activites
System.exit(0); // Releasing resources
You can try starting the Screen 3 with Intent.FLAG_ACTIVITY_CLEAR_TASK http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK
Add android:noHistory="true"
in your activity manifest file.
There are 2 ways for solve your problem
1) call finish() after startActivity(intent) in every activity
2) set android:launchMode="singleInstance" in every tag in menifest file
i think 2nd way is best for solving problem but you can also use first way
public void onBackPressed() {
super.onBackPressed();
finishAffinity();
System.exit(0);
}
may be this method is better usage to close all activity and clean device memory.
来源:https://stackoverflow.com/questions/20210691/how-to-finish-all-activities-and-close-the-application-in-android