2

I use Activity.onUserInteraction() to detect when a user touches any area of the screen when the app is running.

I've noticed that it also get's called when switching between activities (without user touching the screen) therefore leading to false counts of screen touches.

  1. Why is that?
  2. What are other alternatives for getting screen touches?
Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
kev
  • 1,148
  • 2
  • 14
  • 29

1 Answers1

0
  1. Why is that?

I'm not 100% sure, but the documentation for this method gives a potential answer:

This callback and onUserLeaveHint() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notification.

Through this lens, it makes sense that you would get this callback when switching activities, regardless of whether a human interaction caused the switch.

  1. What are other alternatives for getting screen touches?

You could override dispatchTouchEvent(MotionEvent) in a "base" Activity class to be notified of every touch event. This would mean that you'd be called for every event in a single gesture (e.g., you'd be called many times for a single swipe gesture), but you could perhaps decide to react only to ACTION_DOWN events or similar.

Ben P.
  • 52,661
  • 6
  • 95
  • 123