0

I have an application in android that includes a application home page. I override the hardware Home button to go back to the application home page for API level 10 or less.

My application works fine on Android version 3.0 and 4.0. The problem is overriding the Home button. I found a discussion ( Disable Home Button in Android ICS (4.0) ) which includes a method to implement a home screen somehow. It is suggested by @Chalaman.

I did not get the point yet. Is there any one that can help me more by providing some codes?

when we use :

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

in the manifest file, a dialog pops up when we click on the home button. It includes the home phone page icon and application icon. we can make a choice. If we select the application icon, we stay in the application. The problem is we stay also in the same activity. How can I go to another activity (home page of my application)?

We can to it in API level 10 or less:

@Override
    public void onAttachedToWindow() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
            this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow();
    }


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

        }
        return super.onKeyDown(keyCode, event);
    }

    private void loadStartPage() {
        Intent intent = new Intent(getApplicationContext(), StartActivity.class);
        intent.putExtra("user", user);
        intent.putExtra("user_id", user_id);
        intent.putExtra("server", server);
        intent.putExtra("password", password);
        main_activity.startActivity(intent);
    }

How to Load Start Page in API level 11 or greater?

Community
  • 1
  • 1
Ali
  • 9,800
  • 19
  • 72
  • 152
  • Also you should not override the home key at all. The people who built the system meant for applications not to be able to override it. By choosing to do it anyway you are setting yourself up for trouble when they fix the hole that allows you to do it. You should not make your application a Home replacement just so that you can have control of the home key. – FoamyGuy Jun 08 '12 at 00:42
  • Thanks for your suggestion, but still I would to learn – Ali Jun 08 '12 at 00:46
  • It is already answered: http://stackoverflow.com/questions/4944090/disabling-android-home-button-for-industry-application – Adam Arold Jun 08 '12 at 01:16
  • Yes, but that's not what I exactly want. When I click on the icon of the application on the Dialog, I want to go to another activity (Home page application). We stay in the application with the 'intent-filter' but how can we go to another activity? – Ali Jun 08 '12 at 15:17

3 Answers3

2

The answer is simple: don't do it.

Users expect their apps to be consistent across the entire Android platform. I can't imagine a situation in which you could ever justify overriding the home button.


Edit:

To be clear, there are apps out there that appear to override the home button (i.e. the Car Mode app, which won't allow you to escape the app unless you click the "Exit" button). Apps such as these don't actually explicitly override the "home button". You can read more about it here.

Community
  • 1
  • 1
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • Yes you are right, but I would to learn the trick that we can use in these versions. can you help me regarding this? – Ali Jun 08 '12 at 15:27
  • Apparently you can't. Apps that appear to "override the home button" are really just apps that behave like launchers. See this [**post**](http://stackoverflow.com/q/5039500/844882) for more information. – Alex Lockwood Jun 08 '12 at 15:32
  • there are good reasons. for example, what if i wanted to create a "child mode" app that locked the user into a particular app that only provided child-friendly approved content? – Jeffrey Blattman Jun 08 '12 at 15:32
  • True, I didn't mention that. There are certain kind of apps like, for instance, the "Car Mode" app that will allow you to do this, and I don't think the users would complain. That said, these apps don't "override the home button" in the strictest sense. They just behave as launchers. – Alex Lockwood Jun 08 '12 at 15:35
  • Sorry, I'm not sure I understand what you mean. – Alex Lockwood Jun 08 '12 at 15:52
  • I edit the question, you can check what I added to the end of the question. maybe then you can help me. by the way I appreciate for your comments. – Ali Jun 08 '12 at 16:27
1

the android home screen is just an app. it is started by a well-defined broadcast intent that is issued when the home key is pressed.

in a nutshell, you implement an android application that listens to the same event that the stock android home screen app listens to. when the user presses home, they will see the chooser allowing them to select from the stock home screen app, our your app. here's what the intent filter would look like,

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

i realize that's probably not what you want. however, there's no way to completely override the home key. this is intentional as it prevents malicious apps from locking you out of the home screen. the home button is sort of the guaranteed to work "get me out of here" button.

your users can flag your app as the default to handle the home broadcast, but that's probably not what you want either because from then on they want be able to get to the stock home screen unless they clear default or uninstall your app.

preventing users from accessing the home button is a pretty nasty thing to do. are you sure that's what you want?

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • Yes, I would to learn how to implement a home screen instead of overriding the Home button. when I click on the Home botton for example in Android 3.0 the application goes back to the home page of the application. In the Home page if user clicks on the Home button again, we go to the home page of phone. – Ali Jun 08 '12 at 00:42
  • i told you above how to implement a home screen replacement. there's nothing to it. just set up your activity to respond to the intent filter i pasted above. there's nothing special about a so-called home screen app. it's just a regular app that starts itself with a well-defined intent. – Jeffrey Blattman Jun 08 '12 at 00:45
  • I used the intent filter in my manifest file, now when we click on the home button a dialog pops up as you said. But I want to go back to another activity page(application home page) when I click on the logo of my application. Can you help me how to set it? – Ali Jun 08 '12 at 14:03
  • i don't understand what you are asking. as i said, you cannot avoid the chooser when you click home, unless the user selects "user as default" (or however it's worded). i said all this above, and stated the reason for it being so above. – Jeffrey Blattman Jun 08 '12 at 15:30
  • when we use : in the manifest file, a dialog pops up when we click on the home button. It includes the home phone page icon and application icon. we can make a choice. If we select the application icon, we stay in the application. The problem is we stay also in the same activity. How can I go to another activity page in android? – Ali Jun 08 '12 at 15:33
  • when you press your app icon in the chooser, it will start whatever activity has the intent filter associated with it in your manifest. if you want to start a different activity in your app, then move the activity filter to that activity. – Jeffrey Blattman Jun 08 '12 at 17:16
  • No, I tried it with Emulater (both Android Version 3.0, 4.0.3). It did not go to whatever activity has the intent filter associated with it in the manifest. It just stay in the same activity. Maybe it is a problem in the Emulator. have you tested it on Emulator? – Ali Jun 08 '12 at 18:37
  • not sure. have you checked what the intent looks like when your app is resumed? the details of how to implement a launcher replacement app are outside of what i can provide in the comments of a SO post. you have enough to get you started. try googling for some examples, or look at the android source for the launcher. there are lots of examples. – Jeffrey Blattman Jun 08 '12 at 22:49
0

Override onKeydown can't solve your problem .. when you press home button activity calls onPause() .. so put your method inside of onPause().. I guess it will work ..

protected void onPause() {
super.onPause();
 loadStartPage();
}
private void loadStartPage() {
    Intent intent = new Intent(getApplicationContext(), StartActivity.class);
    intent.putExtra("user", user);
    intent.putExtra("user_id", user_id);
    intent.putExtra("server", server);
    intent.putExtra("password", password);
    main_activity.startActivity(intent);
}
Amsheer
  • 7,046
  • 8
  • 47
  • 81