Search code examples
androiddialogoverlayscreenshotdetect

Screenshot overlay not calling onPause() in Activity


I'm trying to show a dialog in my app when a user takes a screenshot. I've added some code to the onResume with a flag and that works fine if the user does something with a screenshot and then comes back into the app (ie shares the screenshotted image).

However, if the user takes the screenshot and an overlay appears with some options (usually Share, Edit, Delete) for a couple of seconds and then disappears if no action is taken, then the onPause of the activity is never called. Any idea how i can detect the screenshot overlay being displayed over my activity?


Solution

  • I fixed it by adding a listener to the activity for focus change:

     @Override
     public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus && hasScreenshotBeenDetected && !showingScreenshotAlert) {
            showScreenShotDialog();
        }
    }