How To Handle Home Button In Android
Solution 1:
You Does not get Home Button click event
.But When u press Home Button
call this method
@OverrideprotectedvoidonStop() {
super.onStop();
}
Solution 2:
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.
Solution 3:
I found the answer pls add code given below-
publicbooleanisApplicationSentToBackground(final Context context)
{
ActivityManageram= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentNametopActivity= tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
returntrue;
}
}
returnfalse;
}
@OverridepublicvoidonStop() {
if (isApplicationSentToBackground(this)){
//put your code here what u want to do
}
super.onStop();
}
make change to manifests file-
<uses-permissionandroid:name="android.permission.GET_TASKS" />
Solution 4:
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.
Solution 5:
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
publicstaticbooleanOnPause=false;
publicstaticbooleanOnResume=false;
put this method in activity
@OverrideprotectedvoidonPause()
{
// TODO Auto-generated method stubsuper.onPause();
OnPause = true;
}
@OverrideprotectedvoidonResume() {
// TODO Auto-generated method stubsuper.onResume();
OnResume = true;
}
@OverrideprotectedvoidonStop() {
// TODO Auto-generated method stubsuper.onStop();
if(OnPause == true && OnResume == false)
{
Log.e("My activity ", " **** home is press *** ");
//Do Your Home press code Here.
}
OnPause = false;
OnResume = false;
}
Post a Comment for "How To Handle Home Button In Android"