Can I override the ‘Home’ button in my application?

The question:

I want to create my own ‘home’ screen on my android, and I want to call that home screen from my application.

How can I override the ‘Home’ button so that when it’s pressed the application will be redirected to my home screen instead of the default home screen? Is it possible to override the home button?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Override the method below.

public void onAttachedToWindow()
    Log.i("TESTE", "onAttachedToWindow");

With this method, the HOME Button stops working in this activity (only this activity). Then you just reimplement as it was a normal button event (the back button for instance).

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HOME) {
        Log.i("TESTE", "BOTAO HOME");
        return true;
    return super.onKeyDown(keyCode, event);    

Method 2

In AndroidManifest.xml

        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.HOME" />
        <category android:name="android.intent.category.DEFAULT" />

You need launchMode="singleTask" so the intent is delivered to the already running app instead of creating a new instance.

In the activity:

    protected void onNewIntent(Intent intent) {
        if (Intent.ACTION_MAIN.equals(intent.getAction())) {
            Log.i("MyLauncher", "onNewIntent: HOME Key");


You do not get a key event

Method 3

The home button is supposed to do one thing and one thing only and consistently. Get the user back to the the HOME screen. Even if you could override it’s behavior it would be an extremely user-unfriendly thing to do. So don’t do it and solve your problem differently!

Method 4

This answer will no longer work, not since Android 4.0.

The correct solution is to create an app that can intercept the Home intent, as per @bara’s answer below.

You can override the home button as any other button:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show();                     
        return true;
    return super.onKeyDown(keyCode, event);

Method 5

Try this in activity

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
            return super.onOptionsItemSelected(item);
    return false;

Method 6

If someone need to detect and ovveride HOME button behavior use thois appproach in KOTLIN

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

class HomeWatcher(context: Context)  {

    val TAG = "hg"
    private var mContext: Context? = null
    private var mFilter: IntentFilter? = null
    private var mListener: OnHomePressedListener? = null
    private var mRecevier: InnerRecevier? = null

    // initializer block
    init {
        mContext = context
        mFilter = IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)

    fun setOnHomePressedListener(listener: OnHomePressedListener) {
        mListener = listener
        mRecevier = InnerRecevier()

    fun startWatch() {
        if (mRecevier != null) {
            this.mContext!!.registerReceiver(mRecevier, mFilter)

    fun stopWatch() {
        if (mRecevier != null) {

    internal inner class InnerRecevier : BroadcastReceiver() {
        val SYSTEM_DIALOG_REASON_KEY = "reason"
        val SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions"
        val SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"
        val SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"

        override  fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            if (action == Intent.ACTION_CLOSE_SYSTEM_DIALOGS) {
                val reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY)
                if (reason != null) {
                    Log.e(TAG, "action:$action,reason:$reason")
                    if (mListener != null) {
                        if (reason == SYSTEM_DIALOG_REASON_HOME_KEY) {
                        } else if (reason == SYSTEM_DIALOG_REASON_RECENT_APPS) {


class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {

    private var launchers = ArrayList<AppLauncher>()
    private var mStoredPrimaryColor = 0
    private var mStoredTextColor = 0
    private var mStoredUseEnglish = false
    private var receiver: BroadcastReceiver? = null

    override fun onCreate(savedInstanceState: Bundle?) {

    fun HomeButtonWatcher()
        val mHomeWatcher = HomeWatcher(applicationContext)
        mHomeWatcher.setOnHomePressedListener(object : OnHomePressedListener {
            override fun onHomePressed() {
                // do something here...
                Toast.makeText(applicationContext, "onHomePressed", Toast.LENGTH_LONG).show()

            override fun onHomeLongPressed() {
                // do something here...
                Toast.makeText(applicationContext, "onHomeLongPressed", Toast.LENGTH_LONG).show()


   // Other code

Method 7

No we cant override home button but i fund a solution for this….:

public boolean isApplicationSentToBackground(final Context context)  {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
    ComponentName topActivity = tasks.get(0).topActivity;
    if (!topActivity.getPackageName().equals(context.getPackageName())) {
        return true;
return false;

public void onStop() {

if (isApplicationSentToBackground(this)){
    //put your code here what u want to do


make change to manifests file-

<uses-permission android:name="android.permission.GET_TASKS" />

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment