简体   繁体   中英

how to finish all activities and close the application in android?

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.

finishAffinity()方法可以完成当前活动和所有父活动,但它仅适用于Android 4.1 或更高版本。

This works well for me.

  • You should using FLAG_ACTIVITY_CLEAR_TASK and FLAG_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 of CloseActivity activity.

     @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); finish(); // Exit }

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

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
}

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);

Sometime finish() not working

I have solved that issue with

finishAffinity()

Do not use

System.exit(0);

It will finish app without annimation.

您可以尝试使用 Intent.FLAG_ACTIVITY_CLEAR_TASK http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK启动 Screen 3

Hi if you are in a fragment and are not able to use the finish method as it is(because finish should solve your problem) then you can use the getActivity.finish() method after startActivity(intent); . If you are not in a fragment you could directly use finish() after you startActivity(intent); line

在您的活动清单文件中添加android:noHistory="true"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM