How To Handle Home Button In Android
Asked Answered
O

5

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);
   }
Overplay answered 3/6, 2012 at 11:12 Comment(1)
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.Shrieval
M
7

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

  @Override
        protected void onStop() {

            super.onStop();
        }
Modestine answered 3/6, 2012 at 11:24 Comment(1)
@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.Onomasiology
M
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.

Manuelmanuela answered 3/6, 2012 at 11:23 Comment(2)
please tell how to call onPause or onstop metohdOverplay
you don't call onPause/onStop, Android calls it for you, you just need to override one of those methods, see here: developer.android.com/reference/android/app/Activity.htmlManuelmanuela
D
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" />
Dira answered 21/6, 2017 at 10:58 Comment(0)
R
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.

Radiolocation answered 27/9, 2019 at 5:31 Comment(0)
O
-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;

}
Onomasiology answered 23/10, 2012 at 13:36 Comment(1)
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.Hyperostosis

© 2022 - 2024 — McMap. All rights reserved.