简体   繁体   中英

Android - Double tap home button as default action

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).

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.

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