2

I Am a New Android Developer, I know Handle The Back Button but I Don't Know How To Handle Home Button, when I Clicked Home Button, I Tried a Lot Of Methods, But Not Used, Please Any One Help To Me and Solve My Problem. I have used following code,

 @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) 
  {
    if(keyCode==KeyEvent.KEYCODE_HOME)
    {
        Toast.makeText(this, "Click Home ", Toast.LENGTH_LONG).show();
    }
    return super.onKeyDown(keyCode, event);
   }
aminography
  • 21,986
  • 13
  • 70
  • 74
mohamed
  • 303
  • 1
  • 6
  • 19
  • I am not shure, but in my opinion there is no way to control home button, because of malware application could be able to dissable last way to user exit appliaction. – neworld Jun 03 '12 at 11:19

5 Answers5

7

You Does not get Home Button click event .But When u press Home Button call this method

  @Override
        protected void onStop() {

            super.onStop();
        }
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
  • 5
    @parag This may create problem for me. when i go to another activity at that time onstop method of 1st activity is call and code execute second time. So, this is a temporarily solution not a perfect working Solution. – Yog Guru Oct 23 '12 at 05:53
0

You can't tell if a HOME button was clicked and you can't stop your app from being hidden when the HOME button is pressed, but you can tell if your app is no longer visible (either BACK key, HOME key, or another app got the foreground).

Just override onPause or onStop, and add a log there.

marmor
  • 27,641
  • 11
  • 107
  • 150
  • please tell how to call onPause or onstop metohd – mohamed Jun 03 '12 at 11:24
  • you don't call onPause/onStop, Android calls it for you, you just need to override one of those methods, see here: http://developer.android.com/reference/android/app/Activity.html – marmor Jun 03 '12 at 11:45
0

I found the answer pls add code given below-

public boolean isApplicationSentToBackground(final Context context) 
   {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}





@Override
public void onStop() {
    if (isApplicationSentToBackground(this)){
        //put your code here what u want to do

    }
    super.onStop();
}

make change to manifests file-

<uses-permission android:name="android.permission.GET_TASKS" />
RkKhanpuriya
  • 164
  • 1
  • 13
0

enter image description here Android Home Key handled by the framework layer you can't able to handle this in the application layer level. Because the home button action is already defined in the below level. But If you are developing your custom ROM, then It might be possible. Google restricted the HOME BUTTON override functions because of security reasons.

Stephan John
  • 331
  • 1
  • 2
  • 11
-1

You Can not detect home press event any more. But you can get home press event by other way Logically It works for me hope useful to you also.

Define this in activity

public static boolean OnPause = false;
public static boolean OnResume = false;

put this method in activity

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

       OnPause  = true;

  }

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

    OnResume = true;        

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

    if(OnPause == true && OnResume == false)
    {
        Log.e("My activity ", " **** home is press *** ");
        //Do Your Home press code Here.

    }

    OnPause = false;
    OnResume = false;

}
Yog Guru
  • 2,074
  • 3
  • 28
  • 47
  • you start the app, which calls onResume and sets OnResume = true. Then you press home button and that calls onPause and sets OnPause = true. Then it goes to OnStop and never gets into IF condition, because both members are true. – MartinC Nov 27 '13 at 16:13