1

currently I'm working on Android development, now I'm facing a problem to exit the whole application that had launched.

I'v tried .finish(), but it doesn't show what I want.

I have 2 Activities, A and B. Activity A will forward to Activity B when button click. In activity B, when I click button "Exit" (that I created) with the listener to trigger .finish(), it just back to Activity A but not to close whole application (what I want is back to home screen directly and kill the background process as well).

How can I exit whole application wherever in the application? Thank you.

button.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            //here to exit whole application not just backwards to previous activity
        }

    });
helloworld1234
  • 327
  • 1
  • 8
  • 19

3 Answers3

0

You write in Activity A after startActivity(new Intent(A.this, B.class)) finish() then you also write in Activity B finish() on button click listener. It should works.

public class A extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   Button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             startActivity(new Intent(A.this, B.class));
             finish(); // you must write this also in A activity to close whole application
        }
    });
 }

    public class B extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_1);
   Button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             finish();
        }
    });
 }
  • Thanks for answering, so it will close Activity A after forward to Activity B, is it? – helloworld1234 Aug 08 '16 at 01:39
  • How if I would like to keep Activity A as well, when whole application is running, which means the back button still can work and back to Activity A, but the exit button in Activity B will close whole application? – helloworld1234 Aug 08 '16 at 01:54
  • Yes it will close Activity A after forward to Activity B. But if you want to keep Activity A running and get there by pressing back button in Activity B and in activity B you have exit button that exit the application. For this purpose you can first remove **finish();** from Activity A and in Activity B you can write **finishAffinity();** in exit button click listener event. It exit the application. but if you press back button it move to Activity A. – Zeeshan Afzal Satti Aug 08 '16 at 05:50
  • For further study refer to [link](https://developer.android.com/reference/android/app/Activity.html#finishAffinity()). If my answer is satisfied you then mark the answer correct and select answer answer is useful. thanxs – Zeeshan Afzal Satti Aug 08 '16 at 05:53
0

EDIT: I assumed there are cases when the underlying activity either should or shouldn't be terminated on return. This will allow you to handle both cases.

Case A)

Activity A starts activity B, which starts activity C. You want to close all of them from activity C. If they're all in the same task (i.e. probably your case) you can close the whole task by calling

finishAffinity();

According to docs this is what happens:

Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.

Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.

See Activity.finishAffinity().

Case B)

Activity A starts activity B, which starts activity C. You want to close activities B and C from activity C.

This is how you start activity C from B expecting and handling a result:

public class ActivityB extends Activity {
    private static final int RC_ACTIVITY_C = 1;
    public static final int RESULT_FINISH = 1;

    ...

    public void startActivityC() {
        Intent intent = new Intent(this, ActivityC.class);
        startActivityForResult(intent, RC_ACTIVITY_C);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_ACTIVITY_C && resultCode == RESULT_FINISH) {
            finish();
        }
    }
}

This is how you let activity B it has to finish from activity C. At any point before finishing activity C call:

setResult(ActivityB.RESULT_FINISH);

See Activity.startActivityForResult(Intent, int) and Activity.setResult(int).

Community
  • 1
  • 1
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
-1

You can try System.exit(0). That should do the job.

EDIT: Take a look at this post for the difference between finish() and System.exit(). Difference between finish() and System.exit(0)

Community
  • 1
  • 1
Hughie Coles
  • 179
  • 1
  • 10
  • 2
    Which will kill the whole process including any content providers and services, potentially running disk writes or network requests. Probably not something you want. – Eugen Pechanec Aug 07 '16 at 16:41
  • System.exit() does not kill your app if you have more than one activity on the stack. See http://stackoverflow.com/a/2632649/2599133 – Hans Brende Aug 07 '16 at 16:47
  • @HansBrende I did not know that, thanks for this info. In any case it's a bad practice and there are more graceful ways. – Eugen Pechanec Aug 07 '16 at 17:03