0

We have an industrial-control app with a main screen containing buttons which launch Activities to control specific industrial processes. The app ships preloaded on a Android device (a Samsung phone) that is only used for this purpose.

In its original version on Android 2.36, API 8, it was still possible to disable the Home button, and we did so, so the user couldn't accidentally exit one of these process activities without going through the main screen. But since then we've upgraded to newer devices running Android 4.42, and to API 16 to take advantage of some newer features, so we can no longer disable the home button. (NB - there's an extensive discussion of the practical impossibility of blocking the Home Button post-API 16 all over SO, but chiefly How to disable Home and other system buttons in Android? and How to disable home button in Android like lock screen apps do? so I consider that question settled) That's OK as long as we can notify the industrial equipment on the other end of the WiFi connection that this has been done so it doesn't think the screen is still up.

So my question is - from my Activity, how can I tell that my Activity is being closed due to the Home button being pressed, and not just some event local to the app? In other words, if the Activity is being closed in the normal fashion it handles onPause() and onStop(), but it also handles those events when the Home button is pressed, so I do I disguish those cases?

user316117
  • 7,971
  • 20
  • 83
  • 158
  • Have you considered making your app the home screen? – tynn Jun 15 '17 at 23:12
  • Well, there are [newer APIs](https://developer.android.com/work/cosu.html) to disable the home and recent buttons. Are you stuck with Android 4.4.2 or can you move to more modern Android devices? – ianhanniballake Jun 15 '17 at 23:35
  • @tynn **1** how would you do that in Android 4.x? (5.x allegedly supports kiosking) ...and... **2** how would that help, since it would still exit me from my activity? – user316117 Jun 16 '17 at 18:39
  • It would help, because you would control the target activity of the home button. – tynn Jun 16 '17 at 22:29

1 Answers1

0
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyCode.KEYCODE_HOME){
        //your code here
    }
}

This should work. link

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
  • No that doesn't work and this SO Question explains why . . . [https://stackoverflow.com/questions/2208912/how-can-i-detect-user-pressing-home-key-in-my-activity](https://stackoverflow.com/questions/2208912/how-can-i-detect-user-pressing-home-key-in-my-activity) ... There are several proposed workarounds there but they all seem kludgy - they all seem to attempt to **infer** the home button rather than actually detecting it. – user316117 Jun 16 '17 at 18:49