Prevent screenshots

If your app contains privacy sensitive data, such as a banking app, a password manager, an app for ‘buying surprise gifts for your loved one’ or an app for looking up the spelling of words like ‘colleague’ - then you might want to add extra protection.

Add the lines below in the onCreate of the Activity you want to protect, and it will prevent screenshots and screenrecordings, and will hide the app’s content from the recent app switcher.

In order to be able to still create screenshots for the app store, disable this security in debug builds:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Prevent screenshots & recordings
        if (!BuildConfig.DEBUG) {
            window.setFlags(
                WindowManager.LayoutParams.FLAG_SECURE,
                WindowManager.LayoutParams.FLAG_SECURE
            )
        }

        ...
    }