Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to finish() the Activity on home button click in android?

Tags:

android

I want to finish the Activity on Home Button Click . I have below code but not get its click event . but still not get clickevent

public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_HOME:
                System.out.println("Home clicked....");
            return true;

            }
        }
        return super.onKeyDown(keyCode, event);
    }
like image 733
Parag Chauhan Avatar asked Mar 27 '12 07:03

Parag Chauhan


2 Answers

When Home button is pressed, onStop method is called in your activity. So what you may do is to add finish(); in onStop method to destroy your activity. Eventually onDestroy method will be raised to confirm that your activity is finished.

like image 108
waqaslam Avatar answered Sep 20 '22 23:09

waqaslam


You can't handle "Home" button.

You could try overriding some of these methods though: onStop, onUserLeaveHint

Are you sure you need to do this though?
If you could save your current state through the onPause/onSaveInstanceState and recover it later the OS should be able to handle the Activity's life cycle and finish it if needed. Can you tell us what is preventing you from doing it that way? :)

like image 43
Samuil Yanovski Avatar answered Sep 19 '22 23:09

Samuil Yanovski