0

Hi in my code I'm trying to catch the home Button click in android i'm using this code for capture the home button click

public boolean isApplicationSentToBackgroundAbove(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;
}

on onPause() function i'm calling this

 if (isApplicationSentToBackground(this)){
    Log.i("Activitymain","Home is press");
    }

This function work fine below 5 OS. (Sony 4.4.2 also does not work) rest of the OS is working

how to solve this issue

Jagan
  • 692
  • 1
  • 12
  • 38
  • 3
    You have *always* been discouraged from using that API for anything beyond curiosity level, and as of API 21 it has been formally deprecated. Focus on why you need to know that the home button has been pressed - most likely, it is an error of failing to design for the way Android is supposed to work. What would be legitimately significant is knowing that your Activity (and perhaps none of the others in your app) is no longer in the foreground, but in the cases where you still have a process capable of knowledge, onPause() provides it. – Chris Stratton Mar 23 '15 at 12:21
  • in my app i need to all one function only then the user press back button or home button so i need this function is there any way to find the home button press – Jagan Mar 23 '15 at 12:24
  • i need to finish the app when user press the home or back button how to solve this – Jagan Mar 23 '15 at 12:29
  • 1
    That's not how Android works. What you should care about is that the user is or is not in one of your Activities, and potentially if your Activity has been finished. Trying to behave differently if, for example, the user leaves by pressing Home vs. by going into something else from the recents key, means trying to create a false distinction incompatible with Android usage flows. Treating the back key uniquely as an exit may be *weakly* legitimate, but you have a means of detecting that. – Chris Stratton Mar 23 '15 at 12:29
  • 1
    Chris is right. I know this is not the answer you're looking for, but instead of asking "how do I do this" you should be asking "what should I do instead". – JHH Mar 23 '15 at 12:35
  • "i need to finish the app when user press the home or back button" -- why? – CommonsWare Mar 23 '15 at 12:57
  • @CommonsWare when ever user goes to the home page and reopen the app I must start the app with splash screen – Jagan Mar 23 '15 at 13:45
  • The simplest solution to get a reliable app is to get rid of the splash screen. Users generally detest splash screens, as they add no value and waste time. – CommonsWare Mar 23 '15 at 14:46
  • Agreed that forcing a splash screen upon users outside of the android lifecycle paradigm is a bad idea, but if that's what you need then it should be possible without having to force the app to be closed when pressing home or back. Using one of the launch modes such as singleTask comes to mind. BTW, did you consider all other ways to navigate away from the app besides home/back? The user could press the recent tasks button, she could pull down the status bar and start something, she could accept an incoming call etc. – JHH Mar 23 '15 at 15:18
  • This is a classic example of asking "how do I do X" instead of "what is the best way to achieve Y". It may turn out Z is a much better way than X to achieve Y, but if you don't tell us what Y is you'll never know. – JHH Mar 23 '15 at 15:19

0 Answers0