21

I am developing the application in which i want to close whole application on button click. I know in android we should not think about to close the application because android does that automatically from this Is quitting an application frowned upon?. but yet i want to close my application.

So what i am doing to close application is i am using Intent.FLAG_ACTIVITY_CLEAR_TOP flag to delete the activity stack.

Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

And in onCreate of FinishActivity.class i am calling this.finish() but application is not get closed and previous activity gets reopened.

FinishActivity.class

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.finish();
}

Update :

Here is the scenario

MainActivity->Activity2->Activity3->FinishActivity

Here Activity2 is gets opened after finishing the activity.

How do i achieve this? Any idea and suggestion will be appreciated.

Thanks & Regards

Community
  • 1
  • 1
Juned
  • 6,290
  • 7
  • 45
  • 93
  • are you doing anything in FinishActivity or just calling this.finish()? – SKK Jan 08 '13 at 06:14
  • just calling `this.finish()` – Juned Jan 08 '13 at 06:16
  • why do you want to keep an activity just to call finish()? just call finish() in Activity3 or any other Activity of yours instead of sending an intent to finishActivity.class. if you really need it, then use the splash screen concept. use a handler thread for 2 or 3 sec and then call finish. – SKK Jan 08 '13 at 06:22
  • Thanks, my main goal is to close whole application on button click – Juned Jan 08 '13 at 06:24
  • try system.exit(1) instead of finish(). – SKK Jan 08 '13 at 06:37
  • No success with `system.exit(1)` – Juned Jan 08 '13 at 06:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22361/discussion-between-santhosh-and-juned) – SKK Jan 08 '13 at 06:48
  • I tried the solution https://stackoverflow.com/a/22881010/955321, and it works for me. – Michael Katkov Jul 05 '17 at 21:21

11 Answers11

35

Give this a try. This should clear your activity stack.

Intent i = new Intent(this,MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
IssacZH.
  • 1,457
  • 3
  • 24
  • 54
27

This finally worked for me 100% of the time. I tried all of the flags standalone, never worked for all instances of the code. Used all the flags everywhere I want this functionality, about 5 different places, and it now works.

            Intent intent = new Intent(actvSignIn.this, actvNearbyPlaces.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            finish();
kennedy484
  • 455
  • 1
  • 5
  • 11
  • I tried so many solutions given in other answers. But this is what worked perfectly! Thanks! – Medha Jan 08 '20 at 06:55
20

Use this--

 Intent intent = new Intent(Activity3.this, FinishActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
 Intent.FLAG_ACTIVITY_NEW_TASK);
 intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
 startActivity(intent);
 finish();

Edited--New Answer and would work perfectly..

just taking an example...do it accordingly what your project needs--

I am taking three Activity class A, B and C..and I have applied a close button on the view of class C Activity. If you want then by the Back button you can go to the previous Activity and when you press the close button then you would exit from apps..have a look--

public class AActivity extends Activity {

  /** Called when the activity is first created. */
  @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent i = new Intent(this, B.class);
    startActivityForResult(i, 1);
}

@Override
protected void onActivityResult(final int requestCode,
        final int resultCode, final Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (resultCode == 5) {
        finish();
    }
}
}

Take next class activity--

   public class B extends Activity {
    /** Called when the activity is first created. */
   @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.b);

    Intent i = new Intent(this, C.class);
    startActivityForResult(i, 2);
}

@Override
protected void onActivityResult(final int requestCode,
        final int resultCode, final Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (resultCode == 5) {
        setResult(5);
        finish();
    }
}
}

Take last activity--

    public class C extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.c);
}

    // close button..set by xml view..you can set it by button listener.
public void close(View v) {
    setResult(5);
    finish();
}
}

Hopefully, it would solve your problem..cheers!

Quick learner
  • 10,632
  • 4
  • 45
  • 55
Hulk
  • 2,565
  • 16
  • 24
  • 1
    Do finish(); before calling next activity...so no need to use anything. – Hulk Jan 08 '13 at 06:18
  • yeah but by using this i won't be able go previous activity using back button – Juned Jan 08 '13 at 06:21
  • Try this instread of finish();-- android.os.Process.killProcess(android.os.Process.myPid()); – Hulk Jan 08 '13 at 06:56
  • this function working fine it quits the FinishActivity but then previous screens is opened mean that Flags are not clearing the stack of activity – Juned Jan 08 '13 at 11:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22383/discussion-between-juned-and-hulk) – Juned Jan 08 '13 at 11:08
  • 2
    hey juned i resolved your problem--- use startActivityForResult(); instread of using startActivity();...ok use this on every activity when you start and set the result.. – Hulk Jan 09 '13 at 05:26
4

From documentation for Intent.FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

So to get it work your FinishActivity must be first one in your Activity stack. In any other cases this solution wouldn't give you anything.

You must perform several steps to perform this:

1) Make FinishActivity as your launcher activity.

2) Do not provide any view for it and start first Activity of your application directly from onCreate callback :

3) Redefine onRestart callback:

Code sample:

private boolean isNeedToContinue = true;

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
}

@Override
protected void onResume() {
    super.onResume();
    if (isNeedToContinue) {
                    startActivity(new Intent(this,FirstVisibleActivity.class));
                    isNeedToContinue = false;
    } else {
        finish();
    }
}

@Override
protected void onRestart() {
    super.onRestart();
    finish();
    isNeedToContinue = false;
}

I guess this is all you need. Good luck!

Evos
  • 3,915
  • 2
  • 18
  • 21
3

setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TOP);

apart from that set launchMode to "singleTop" in FinishActivity definition in xml, overwrite onNewIntent method , you can pass some additional information as part of intent , instated of finishing your activity in onCreate finish it in onNewIntent method of activity based on some signal from calling activity or simply finish it based on your need . It maybe possible your other activities have different lauchmodes that's why they are not finishing .

android2013
  • 415
  • 2
  • 5
  • i haven't set any launch modes to other activities and i have already tried with setting a launchmode to `SingleTop` to `finishActivity.class` in manifest. – Juned Jan 08 '13 at 06:28
  • you can try this : set launchMode to "singleTop" in FinishActivity definition in xml, overwrite onNewIntent method and finish your activity there . – android2013 Jan 08 '13 at 07:03
  • How to overwrite that in onNewIntent, can you please make it clear ? – Juned Jan 08 '13 at 11:07
  • Should be used in your activity class @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); } – android2013 Jan 14 '13 at 08:04
2

As other answers have the following should work.

Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

For API level < 11, Intent.FLAG_ACTIVITY_CLEAR_TASK is not available. Instead I used IntentCompat.FLAG_ACTIVITY_CLEAR_TASK from support library.

See IntentCompat

Might help someone stumbling across.

0

Try Like this Way this is work for me:

Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

instead of your code

Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Dixit Patel
  • 3,190
  • 1
  • 24
  • 36
0

Try this:

Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
SKK
  • 5,261
  • 3
  • 27
  • 39
  • are you doing anything in FinishActivity or just calling this.finish()? – SKK Jan 08 '13 at 06:15
  • could you check whether you are using intent.setFlags or intent.addFlags. – SKK Jan 08 '13 at 06:17
  • after calling finish in FinishActivity, are u seeing Activity3 or someother activity. ? – SKK Jan 08 '13 at 11:10
  • Activity2 see my update, i have created simple app to test this – Juned Jan 08 '13 at 11:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22385/discussion-between-juned-and-santhosh) – Juned Jan 08 '13 at 11:15
  • everytime you start another activity using intent use intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); I mean when u start Activity3 from Activity2 use the same line. also from MainActivity to Activity2. – SKK Jan 08 '13 at 11:15
  • Still no change having same problem – Juned Jan 08 '13 at 11:59
0

This worked for me:

Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Franrc
  • 286
  • 3
  • 5
0

Try this: It worked for me

Intent intent = new Intent(this, MoreOptionsActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
Rahul Dhrub
  • 71
  • 1
  • 8
-1

Also you can use finishAffinity() function in last activity like:

finishAffinity()
startHomeActivity()

Hope it'll be useful.

Djek-Grif
  • 1,391
  • 18
  • 18