0

I want to force exit my android app when it's going to background.So i use this code in my base Activity class.

@Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_HOME) {
            finish();
            System.exit(0);         
        }
        return super.onKeyDown(keyCode, event);
    }

But it's not working.Please help for resolving this.

Arun C
  • 9,035
  • 2
  • 28
  • 42
  • 2
    why would you want to kill the background activity? – Raghunandan May 31 '13 at 05:15
  • Do you want to close the the whole application or just the one activity ? – user1721904 May 31 '13 at 05:16
  • You cannot intercept Home. The OS won't let you as an anti hijacking measure. Not a good idea to call exit() either. Try the onStop() method in Basim's answer. – Torp May 31 '13 at 05:19
  • `System.exit(0)` is not a good way to exit. use `finish()` – Bishan May 31 '13 at 05:21
  • i want to close the whole app – G Ekanayake May 31 '13 at 05:32
  • @user1621952 http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon. Check the link and the answer by commonsware. You should leave that decision to the os.. Also chekc this in case you are looking for navigation flow http://developer.android.com/design/patterns/navigation.html – Raghunandan May 31 '13 at 05:49

1 Answers1

1

You need to do like this,

    @Override
public void onStop(){
    super.onStop();
    finish();
}

This will override your Onstop method

Basim Sherif
  • 5,384
  • 7
  • 48
  • 90