1

When trying to look up how to block screenshots on Android, I noticed that many sites suggest[0] setting FLAG_SECURE on the window and before calling setContentView().

I have been trying to understand why, but I'm not able to find anything. Looking at the source code for Window.setFlags(), I can't see any difference Activity.setContentView() would make.

[0]: example: https://commonsware.com/blog/2012/01/16/secure-against-screenshots.html , https://stackoverflow.com/a/29164604/1574325

Community
  • 1
  • 1
Rohan Dhruva
  • 1,194
  • 1
  • 10
  • 20
  • 1
    You have to set the window flags before calling `setContentView()`. Otherwise, you will get an exception. As to "why" beyond that, I have no idea. I treat it as "just one of those things" and move along to other work... :-) – CommonsWare May 09 '17 at 18:30
  • @CommonsWare, interesting: I don't see an exception, but my use is slightly different: I have an activity, which has a fragment, inside of which is a ViewPager of images. In the view pager, there might be certain items that I don't want the user screenshotting, so when such an item shows, I do: `fragment.getActivty().getWindow().addFlags(FLAG_SECURE)`. This does not throw an exception, but doesn't work on some older devices. I wonder if they're related that way. – Rohan Dhruva May 09 '17 at 19:51
  • 2
    Hmmm... perhaps it's only some flags that you need to set before `setContentView()`. I'm really sure that there are scenarios where calling `addFlags()` after `setContentView()` throws an exception. – CommonsWare May 09 '17 at 20:24

0 Answers0