android intercept recent apps button

After lots of searching and coding the current best solution I found is the following:

public void onWindowFocusChanged(boolean hasFocus) {

    if (!hasFocus) {
        windowCloseHandler.postDelayed(windowCloserRunnable, 0);

private void toggleRecents() {
    Intent closeRecents = new Intent("");
    ComponentName recents = new ComponentName("", "");

private Handler windowCloseHandler = new Handler();
private Runnable windowCloserRunnable = new Runnable() {
    public void run() {
        ActivityManager am = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;

        if (cn != null && cn.getClassName().equals("")) {

This requires that you use <uses-permission android:name="android.permission.GET_TASKS" />

When using this approach when the user presses the recent apps button it will cause your activity will go through the activity lifecycle as follows: onPause -> onWindowFocusChanged -> onResume.

To the user the behavior appears that pressing the recent apps button has no response. NOTE: that I have found that if you press the recent apps button quickly it will display that view for brief time.

This is not the best solution, but it is a stab at it. If you have a better solution please share.

Leave a Comment