简体   繁体   中英

Set Action for home button in android

How I can change working of home button in android? I want to when I click on home button I do some actions and after that application should go to background. How I can do that.

Write your own home screen. When the user presses HOME, they will get a choice of running your app or the built-in home screen. They can elect to choose one just this one time, or set either app as being their default from now on. Many will wonder why on Earth you decided to decided to implement a home screen.

Most developers care about any case where their activity moves to the background, in which case you can either use a lifecycle method (eg, onPause() , onStop() ) or try onUserLeaveHint() .

Have you tried overriding onKeyDown()

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HOME) {
        // ENTER CODE HERE
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

I do it this way:

/* Handles item selections */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:    
                    //You can do whatever you want here       
            Intent homeInt = new Intent(this, SomeActivity.class);
            startActivity(homeInt);
            return true;
        }
        return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM