Black Screen while Using Windowmanager.LayoutParams.FLAG_SECURE in google pixel 6 (android >= 11)

  1. I face a black screen whenever I use Windowmanager.LayoutParams.FLAG_SECURE to prevent screenshot and screen recordings on a device. This bug happens in specific devices like Google Pixel 6, Oppo, and Vivo.
  • The following app activates Windowmanager.LayoutParams.FLAG_SECURE on clicking a button and then screen turns black. The code is below: -
  • App.js: -
  • /* eslint-disable react/react-in-jsx-scope */
  1. import {Button, StyleSheet, View} from ‘react-native’;
  2. import * as ScreenCapture from ‘expo-screen-capture’;
  • export default function ScreenCaptureExample() {
  1. const activate = async () => {
  2. await ScreenCapture.preventScreenCaptureAsync();
  3. };
  • const deactivate = async () => {
  1. await ScreenCapture.allowScreenCaptureAsync();
  2. };
  • return (
  1. );
  2. }
  • const styles = StyleSheet.create({
  1. container: {
  2. flex: 1,
  3. alignItems: ‘center’,
  4. justifyContent: ‘center’,
  5. },
  6. });
  7. ScreenCaptureAndroid.js: -
  • import * as ScreenCapture from ‘expo-screen-capture’;
  • const allowScreenCapture = async () => {
  1. try {
  2. await ScreenCapture.allowScreenCaptureAsync();
  3. } catch (e) {}
  4. };
  5. const disableScreenCapture = async () => {
  6. try {
  7. await ScreenCapture.preventScreenCaptureAsync();
  8. } catch (e) {}
  9. };
  • const func = {
  1. allowScreenCapture,
  2. disableScreenCapture,
  3. };
  4. export default func;
  5. ScreenCaptureModule.kt (File from expo-screen-capture node_modules folder): -
  • package expo.modules.screencapture
  • import android.view.Display
  • import android.content.Context
  1. import android.view.WindowManager
  2. import expo.modules.kotlin.exception.Exceptions
  3. import expo.modules.kotlin.functions.Queues
  4. import expo.modules.kotlin.modules.Module
  5. import expo.modules.kotlin.modules.ModuleDefinition
  • class ScreenCaptureModule : Module() {
  1. private val context: Context
  2. get() = appContext.reactContext ?: throw Exceptions.AppContextLost()
  3. private val currentActivity
  4. get() = appContext.currentActivity ?: throw Exceptions.MissingActivity()
  • override fun definition() = ModuleDefinition {
  1. Name(“ExpoScreenCapture”)
  • OnCreate {
  1. ScreenshotEventEmitter(context, appContext.legacyModuleRegistry)
  2. }
  • AsyncFunction(“preventScreenCapture”) {
  1. currentActivity.window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
  2. }.runOnQueue(Queues.MAIN)
  • AsyncFunction(“allowScreenCapture”) {
  1. currentActivity.window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
  2. }.runOnQueue(Queues.MAIN)
  3. }
  4. }