-1

Does anyone know how to kill application after onBackpress on Android ? I using this code to close my application.

  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
                && keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            Log.d("CDA", "onKeyDown Called");
            onBackPressed();

        }

        return super.onKeyDown(keyCode, event);
    }

    public void onBackPressed() {

            Log.d("CDA", "onBackPressed Called");
            Intent setIntent = new Intent(Intent.ACTION_MAIN);
            setIntent.addCategory(Intent.CATEGORY_HOME);
            setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(setIntent);
      return;

    }

My case , after I close the application using backpress the application not kill the application. I mean after I close the app and I open it again the application start with the last (acitivity) where I close the application (using backpress)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Riken
  • 47
  • 8
  • https://stackoverflow.com/a/27765687/7339411 – Curio Jul 11 '20 at 10:21
  • @Curio it's work when I'm using this.finishAffinity(); after startActivity(setIntent). Thanks – Riken Jul 11 '20 at 10:35
  • If you managed to solve your problem, do not edit your question to 'fix' the problem in the question (invalidating the question) nor add things like "[SOLVED]" to the title. Instead **accept** the answer that helped you solve the problem, or - if there is no such answer - post your own answer. Accepting an answer is what marks a question as solved. I have rolled back your question to its initial state. – Mark Rotteveel Jul 11 '20 at 11:02

2 Answers2

1

Use this.finishAffinity(); after startActivity(setIntent); in your case, as suggested in this answer here stackoverflow.com/a/27765687/7339411

Note that since API 21 you can use finishAndRemoveTask(); as well.

Curio
  • 1,331
  • 2
  • 14
  • 34
-1

Use finish() inside onBackPressed().

Samudra Ganguly
  • 637
  • 4
  • 24
  • This answer should have been [expanded like this one](https://stackoverflow.com/a/16480930/1291879). As it is, it should have of been a comment. – Tigger Jul 11 '20 at 10:14