---
title: "How can I detect user pressing Home button in Android"  
description: "How can I detect user pressing Home button in Android"  
author: "Manoj Bhatt"  
published: 2015-12-09  
updated: 2023-06-27  
canonical: https://www.mindstick.com/forum/33708/how-can-i-detect-user-pressing-home-button-in-android  
category: "android"  
tags: ["android", "android controls"]  
reading_time: 3 minutes  

---

# How can I detect user pressing Home button in Android

I have used below code. But its not working.\

```
@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_HOME:

Toast.makeText(getApplicationContext(), "Home button pressed", Toast.LENGTH_SHORT).show();

			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
```

## Replies

### Reply by Aryan Kumar

Sure, here is how to do date comparison validation in Android:

Code snippet

```plaintext
import java.util.Calendar;
import java.util.Date;

public class DateComparisonValidation {

    public static boolean isDate1AfterDate2(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);

        return calendar1.after(calendar2);
    }

    public static void main(String[] args) {
        Date date1 = new Date();
        Date date2 = new Date();

        if (isDate1AfterDate2(date1, date2)) {
            System.out.println("Date1 is after Date2");
        } else {
            System.out.println("Date1 is not after Date2");
        }
    }
}
```

In this code, we first create two Date objects, `date1` and `date2`. Then, we call the `isDate1AfterDate2()` method to compare the two dates. The `isDate1AfterDate2()` method returns a boolean value, which indicates whether `date1` is after `date2`.

Finally, we print a message to the console to indicate the result of the comparison.

Here is an explanation of the code:

- The `Calendar.getInstance()` method returns a Calendar object that represents the current date and time.
- The `setTime()` method sets the Calendar object to the specified date.
- The `after()` method returns a boolean value, which indicates whether the specified date is after the current date.

### Reply by Manoj Pandey

The Home button is a very dangerous button to override and, because of that, Android will not let you override its behavior the same way you do the BACK button.

Use below code to find home button press.

Create class **HomeWatcher** class.

```
package com.example.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

public class HomeWatcher {

	static final String TAG = "hg";
	private Context mContext;
	private IntentFilter mFilter;
	private OnHomePressedListener mListener;
	private InnerRecevier mRecevier;

	public HomeWatcher(Context context) {
		mContext = context;
		mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
	}

	public void setOnHomePressedListener(OnHomePressedListener listener) {
		mListener = listener;
		mRecevier = new InnerRecevier();
	}

	public void startWatch() {
		if (mRecevier != null) {
			mContext.registerReceiver(mRecevier, mFilter);
		}
	}

	public void stopWatch() {
		if (mRecevier != null) {
			mContext.unregisterReceiver(mRecevier);
		}
	}

	class InnerRecevier extends BroadcastReceiver {
	final String SYSTEM_DIALOG_REASON_KEY = "reason";
	final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
	final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
	final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
		String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
		if (reason != null) {
			Log.e(TAG, "action:" + action + ",reason:" + reason);
				if (mListener != null) {
		if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
					mListener.onHomePressed();
		} else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
					mListener.onHomeLongPressed();
						}
					}
				}
			}
		}
	}

	public interface OnHomePressedListener {
		public void onHomePressed();

		public void onHomeLongPressed();
	}
}
```

Now call on your activity

\

```
HomeWatcher mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
			@Override
			public void onHomePressed() {
				// do something here...

			}

			@Override
			public void onHomeLongPressed() {
			}
		});
		mHomeWatcher.startWatch();
```

\


---

Original Source: https://www.mindstick.com/forum/33708/how-can-i-detect-user-pressing-home-button-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
