26

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
rahul
  • 2,613
  • 8
  • 32
  • 55

9 Answers9

87

There is finishAffinity() method that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ragaisis
  • 2,700
  • 1
  • 26
  • 41
  • 4
    still leaves children activities... ex: mainactivity launchmode is singleInstance, settings menu is launched, services calls startactivity on mainactivity, mainactivity calls finishAffinity... settings menu is not finished... this is not contrived, i'm writing an app launched and finished by bluetooth... seems like broadcastreceivers in each open activity are required to make sure everything closes in all situations – me_ Jun 26 '18 at 20:39
  • finishAffinity / finish works on the back-stack. Its not necessarily killing its parent activity. The child activity can get launched in a separate task.. – ani0904071 Jun 14 '23 at 15:34
16

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 
    }
    
ByteHamster
  • 4,884
  • 9
  • 38
  • 53
Loi Ho
  • 414
  • 4
  • 9
7

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
Sheikh Hasib
  • 7,423
  • 2
  • 25
  • 37
3

Sometime finish() not working

I have solved that issue with

finishAffinity()

Do not use

System.exit(0);

It will finish app without annimation.

AJay
  • 1,233
  • 10
  • 19
2

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);
krlzlx
  • 5,752
  • 14
  • 47
  • 55
Avnish Choudhary
  • 828
  • 7
  • 15
1

Add android:noHistory="true" in your activity manifest file.

Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
1

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

Ankur Kumar
  • 972
  • 7
  • 12
1

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

Joel
  • 353
  • 2
  • 7
0

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

Mitul Gedeeya
  • 886
  • 1
  • 10
  • 20