Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I override the 'Home' button in my application?

Tags:

android

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?

like image 636
Smith Avatar asked Apr 05 '11 05:04

Smith


3 Answers

Override the method below.

@Override
public void onAttachedToWindow()
{  
    Log.i("TESTE", "onAttachedToWindow");
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.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);    
}
like image 139
lucasneo Avatar answered Nov 20 '22 04:11

lucasneo


In AndroidManifest.xml

<activity
    ...
    android:launchMode="singleTask">
    <intent-filter>
        <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" />
        ....
    </intent-filter>
</activity>

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

In the activity:

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

        }
    }

You do not get a key event

like image 37
bara Avatar answered Nov 20 '22 03:11

bara


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!

like image 6
markus Avatar answered Nov 20 '22 05:11

markus