4

I wanted to call certain function for showing notifications on pressing of homebuttonclick event.

I refered this blog.

Written code as:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_HOME){
            APP_STATUS="SLEEP";

          LocalToNotification();

        }
        return true;
    }   

But unfortunatly it was not working.

I tested through debugger, but observed that debugger is also not comming on this line of code upon pressing home button.

What is wrong in above code?

Please help me.

C Sharper
  • 8,284
  • 26
  • 88
  • 151

4 Answers4

8

On older Android version this is working. But Android changed this, because they say "Home Button should stay Home Button" and they don't want that anybody override the Home Button. And because of this reason your code is not working anymore.

If you want to do something when the home button is pressed, then do this in the onPause method.

silvia_aut
  • 1,481
  • 6
  • 19
  • 33
  • sir , with onpause it really worked, but dont know how it does not worked for code in the blog – C Sharper Sep 19 '13 at 11:57
  • As I said, Android don't want to do somthing on the Home Button. If you take a look at the link you will find just 6 keyevents and the home button is not listed because of that reason that it shouldn't be overrided. http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.Window-event-android:back – silvia_aut Sep 19 '13 at 12:01
  • i am doing t in onpause but it takes long time to again come to front . actually i am creating a screen lock app. @silvia_aut – Sagar Nayak May 22 '16 at 06:04
2
protected void onUserLeaveHint() {

    finish();
    super.onUserLeaveHint();
}
vjdhama
  • 4,878
  • 5
  • 33
  • 47
2

Key code constant: Home key. This key is handled by the framework and is never delivered to applications.

Suriv
  • 51
  • 4
1
protected void onUserLeaveHint ()

Added in API level 3

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.

This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.

code:-

    @Override
    protected void onUserLeaveHint()
    {
        // TODO Auto-generated method stub
        super.onUserLeaveHint();

        new OfflineAsyncTask(BaseActivity.this).execute();
    }
duggu
  • 37,851
  • 12
  • 116
  • 113