rahul
rahul

Reputation: 2661

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.

Upvotes: 27

Views: 54624

Answers (9)

Joel
Joel

Reputation: 483

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

Upvotes: 1

Ragaisis
Ragaisis

Reputation: 2750

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

Upvotes: 89

Sheikh Hasib
Sheikh Hasib

Reputation: 7493

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

Upvotes: 7

AJay
AJay

Reputation: 1193

Sometime finish() not working

I have solved that issue with

finishAffinity()

Do not use

System.exit(0);

It will finish app without annimation.

Upvotes: 3

Avnish Choudhary
Avnish Choudhary

Reputation: 838

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

Upvotes: 2

Loi Ho
Loi Ho

Reputation: 414

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 
    }
    

Upvotes: 16

Ankur Kumar
Ankur Kumar

Reputation: 972

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

Upvotes: 1

Mitul Gedeeya
Mitul Gedeeya

Reputation: 896

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

Upvotes: 0

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

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

Upvotes: 1

Related Questions