Android - Double tap home button as default action

478 views Asked by At

I am currently implementing an android application. When I press the home/overview buttons, the application pauses by default (showing the applications overview for the second one). I have noticed that most of the applications in recent android versions, give the ability of double tap on home and overview buttons. Specifically, when home/overview buttons are pressed, it pops up a toast message "Tap again to exit". Is this the default action of home/overview buttons. If so, how can I enable it in my application? If not, could I override the default behavior? It is mentioned in various posts that there is no ability of replacing home/overview button actions, since they are system default (that's why there are no handling methods like onBackPressed for back button).

1

There are 1 answers

2
private static On

In your MainActivity, inside onPause/onResume methods.

@Override
    protected void onPause() {
        showToast("You paused the activity");
        super.onPause();
    }
    
    @Override
    protected void onResume() {
        showToast("You resumed the activity");
        super.onResume();
    }
    
    
    private void showToast(String str) {
        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
    }

The previous methods will display a toast message saying you paused the activity when you press the back, home or overview buttons (onPause). Similarly if you re-enter the app, it will display you resumed the activity.