Overriding the Home button - how do I get rid of the choice?
Asked Answered
P

4

21

When creating one Intent so that MyActivity reacts to a User pressing the Home-button is easy using the XML markup:

    <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>

I want to know how to avoid getting the choice of "what activity do you want to use" for the Home screen? HTC has made its "Touch Flo" (Sense) override the default "start" Activity and I never get the question if I want to use "Start" or "TouchFlo" usually. However, when I added my own Activity I always get the question.

Yes, I know that I can check the "Use this as standard"-checkbox, but that's not what I want right now. So, question is: can I make the system override everything else and always use MyActivity as default?

Next, I really only want to override the normal Home Screen when my app is running. If its not running, everything should work as normal, ie MyActivity should NOT be associated with the Home button.

Premillennialism answered 17/1, 2010 at 1:53 Comment(3)
The only reason you don't get the activity picker dialog when you press home on an HTC device is because they've preinstalled it with that decision made. You can see that HTC are subject to the same Activity picker by going to to Applications > TouchFLO > Clear Defaults, then press Home. You'll be prompted to use either the HTC home screen or the stock Android one.Tap
Ok, so we're sort of stuck with either ALWAYS override the default home screen or dont override the button at all... =(Premillennialism
Related: Can I override the 'Home' button in my application?, How to disable the home keySchoen
H
30

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.

Haukom answered 17/1, 2010 at 7:54 Comment(5)
Thx for the answer. My application is not an ordinary app for everyone. It is a very specific app used in a business environment and thus has very specific requirements. In this app we use other external apps that we want to "integrate" which complicates it even more. We need to be able to go to this external app (nav software) and then back to our program "seamlessly". Since the nav app is fullscreen, we need to override a Hardware button to be able to return to our main app, thus the question. BACK-button wont work, its alreay overridden in the nav app it seems...Premillennialism
I really wanted a "header" on top in the nac software, but that didnt work so I need a button. HOME-button was the natural choice since that would take the User back to the "main activity" of the app...Premillennialism
I don't know who you work for, but based on all your questions here on StackOverflow, I previously worked on a ridiculously similar product including all the lock-down requirements and what-not. If you want to implement this kind of fully-integrated solution on Android you will have to modify the Android platform itself and provide a custom image that you would need to flash to each device.Tap
your answer might be correct but not fully correct. the screen lock apps override the home button right ? how do they do it ?Iambic
I'll give you an example how HOME button needs to be suppressed. I'm developing on a barcode scanner and it's scan button is right under home button. One slip and you're back to desktop...Herman
T
2

The overridable onUserLeaveHint() lifecycle hook can be used when the user presses the home button.

This hook has been present since API level 3.

@Override
protected void onUserLeaveHint() {
    super.onUserLeaveHint();
    // Your code here
}

More info about the method:

Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice.

For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted.

In cases when it is invoked, this method is called right before the activity's onPause() callback.

Source: https://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()

Ternion answered 22/11, 2016 at 15:53 Comment(0)
D
1

Well, the user would still have to choose the regular home as their home with use as default checked to stop the prompt from coming back. However, I believe it is possible for you to then modify the system settings in some way to incidentally have your particular activity be considered the default home, such that press of home would then do nothing or appear locked to the user at the main activity, and I understand you wish to launch your user into other activities from there, giving them a fast home press return to this root Launcher. I can see the benefit of this completely, and it may even benefit what I'm developing if I choose to implement multiple widget screens that the user can flip left or right between.

There is at least 1 app out there that I've downloaded which appears to be doing exactly this. When the user exits the app, it restores the user's regular default home preference. The app instructs the users very explicitly to choose Launcher as their default home and never do that with the app itself. It has a few different "exit" methods which give the user the choice of which home to return to if they have multiple, and one that just exits to their regular default.

I am researching this as well and will report back with progress & source.

Day answered 10/3, 2010 at 16:6 Comment(0)
S
0

Use onUserLeaveHint().

onUserLeaveHint() is a protected method as other lifecycle methods of the activity and if you are handling onUserLeaveHint this will take care of the following case

  1. When User hits home key
  2. When User hits back key
  3. When User hits annunciator bar

Here you can catch Homeb button click.

Sylvia answered 30/6, 2014 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.