0

I've spent a few hours looking for this. My test device is a nexus 6, though it has been tried on android 4.4 and 5.0+ as well.

Basically I want to catch a user's click of the onBackPress, but I want to do this outside of the activity. Say I've got an object that is initialized and while its running, It is to handle onBackPress, until the its killed.

I've looked into setting an onKeyListener to the contentView but that does not work at all (I figured as much, but its worth a shot).

Any idea how to do this (again, outside the scope of overriding in the activity)?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
miversen33
  • 573
  • 5
  • 19
  • Maybe you could tell us a little bit about, what you're trying to accomplish - there might be another solution for your issue. As Max Benson says, this isn't really possible and why should it even be possible? – Darwind Mar 21 '15 at 23:49
  • I've got a view that creates another view over everything (a popup if you will) and I need the back button to get rid of it. I am fully aware of popupWindow and have decided against it. I have debated making an activity to do this but am not sure how I would pass the views from the object to the activity. – miversen33 Mar 22 '15 at 00:09
  • possible duplicate of [Android Overriding onBackPressed()](http://stackoverflow.com/questions/18337536/android-overriding-onbackpressed) – Jared Burrows May 30 '15 at 18:29
  • That is overriding inside an activity. My question States that I needed to be able to do it from an object outside of an activity – miversen33 May 30 '15 at 22:10

1 Answers1

0

I cannot imagine something like that being possible since it would be a rather large security risk if a regular application could just catch user input outside of its scope.

The only hardware button I know of that you can detect being pressed even when your activity is not running in the foreground is the camera button, since pressing that generates an Intent. http://developer.android.com/reference/android/content/Intent.html#ACTION_CAMERA_BUTTON

Maximilian Schier
  • 1,579
  • 14
  • 18