Can I override the 'Home' button in my application?
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:
@Override
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);
}
Overriding the functionality of Home Button
Try this:
@Override
protected void onUserLeaveHint() {
if (!navigating) {
Intent intent2 = new Intent();
intent2.setClass(ActivityA.this, ActivityB.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
forceHome(this, intent2);
}
super.onUserLeaveHint();
}
public static void forceHome(Context paramContext, Intent paramIntent) {
if (paramIntent != null) {
((AlarmManager) paramContext.getSystemService(ALARM)).set(1,
System.currentTimeMillis(),
PendingIntent.getActivity(paramContext, 0, paramIntent, 0));
}
}
As a security feature of android, the activity only launches after 5 seconds. If you want to launch it immediately. use your own Home Launcher
.
Override Home Button Android
You can make your application as a Home screen launcher app using very similar activity code in your AndroidManifest. This way when user press the home button your app will be opened.
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- The following two intent-filters are the key to set homescreen -->
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Overriding the Home button - how do I get rid of the choice?
You can't permanently override the Home button without the user confirming it.
One argument for why this is the case is a security one. The Home button is the one way a user can be guaranteed to exit any application. If you could make the Home button launch your application instead of the Home screen without the user confirming this change, it would then be very easy to write a malicious application that hijacked a user's phone.
Alternatively, your application could contain a replicate Home Screen that harvested a user's Google account details; not that hard since the source is available. If your application could silently replace the default Home Screen, it would be hard for the user to tell this had happened.
Also, do you really want to override Home? Home is like an emergency escape button. Pressing Home twice will always take a user back to the center panel of the Home Screen, so whatever application they're running, it's easy for a user to get back to somewhere they know. You shouldn't really be overriding this unless you're producing a full Home replacement.
Override longpress Home Button Action (Assistant)
Is it possible to override the long press behavior.
You can create your own assistant. The user can then opt into using your assistant, instead of Now On Tap, via the Settings app.
Otherwise, you have no means of intercepting a long-press on navigation buttons like HOME.
If yes which intent filter is it?
Um, it's significantly more complicated than that, and there's no activity required in the process. The limited documentation can be found here, and this sample project of mine implements one.
Related Topics
Published App on Play Store Can't Communicate with Google Maps API and Facebook API
Too Many Field References: 70613; Max Is 65536
Navigationview Onnavigationitemselectedlistener Not Being Called
Listview Click to Show Image in Imageview
Valid Values for Android:Fontfamily and What They Map To
Refreshing Oauth Token Using Retrofit Without Modifying All Calls
Android Paint: .Measuretext() VS .Gettextbounds()
How to Achieve Ripple Animation Using Support Library
How to Serialize an Object and Save It to a File in Android
How to Use Addr2Line in Android
Formula Px to Dp, Dp to Px Android
Start an Activity with a Parameter
Show Alertdialog in Any Position of the Screen
How to Remove Application from App Listings on Android Developer Console
Appcompat V7 R21 Returning Error in Values.Xml
How to Handle an Asynctask During Screen Rotation
Accessing Localhost of Pc from Usb Connected Android Mobile Device