title: What's the best way to log time spent on various screens? (Android, Java)
body: <p>I'm working on a simple gallery and would like to log which pictures people look at a lot (so I can make those images easier to find - think inferred favorites).</p>
  
  <p>On the surface, this seems pretty simple - look at the time when a media item is opened, look at the time again when they hit close or swipe to another item; log the delta against that media ID, done. But this seems really naive - what happens if a user locks their screen, or backgrounds the app? There are lots of ways to <em>stop</em> looking at an image, and trying to manually enumerate them all is pretty fragile and would depend on hooks that might not even exist.</p>
  
  <p>This seems like a common enough problem that there's likely to be an elegant solution, but it's really hard to Google (try searching "Android log screen time java" and you'll see what I mean). Is there a common pattern here, or another term that describes what I'm trying to do? My dream is some for-purpose method that I can spawn when the page loads, which will get a callback / a tiny bit of execution when the page is destroyed - no matter what user action caused that to happen.</p>