I'm working on an Android app where I need to block the app screen from being visible when the user switches to the app switcher (recent apps view) but still allow the user to take screenshots and record the screen when the app is in the foreground. The goal is to secure the app's content from being seen by third parties when the app is in the background or when it's being viewed in the app switcher.
FLAG_SECURE
in onPause()
and onResume()
:FLAG_SECURE
in the onPause()
method and turning it off in onResume()
. This approach works like 2 times out of 5, but occasionally, the flag doesn't block the content when the app is switched to the background quickly. This results in the app's content being visible in the app switcher for a brief moment.@Override
protected void onPause() {
super.onPause();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
}
@Override
protected void onResume() {
super.onResume();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
}
This approach blocks the screen from being visible in the app switcher in many cases but is inconsistent. Sometimes, it fails to block the screen from showing in the app switcher, especially when switching quickly between apps.
FLAG_SECURE
in onCreate()
:FLAG_SECURE
in onCreate()
, which effectively blocks the content from being visible in the app switcher. However, this approach also blocks screenshots and screen recordings entirely, which is not acceptable for my use case. I want the user to still be able to take screenshots or record the screen when the app is in the foreground.@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
}
import React, { useEffect, useState } from 'react';
import { AppState, View, Text, StyleSheet } from 'react-native';
const App = () => {
const [appState, setAppState] = useState(AppState.currentState);
const [isBlurred, setIsBlurred] = useState(false);
useEffect(() => {
const handleAppStateChange = (nextAppState) => {
if (nextAppState === 'background' || nextAppState === 'inactive') {
setIsBlurred(true); // Show blur when app is in background
} else {
setIsBlurred(false); // Remove blur when app returns to foreground
}
};
const subscription = AppState.addEventListener('change', handleAppStateChange);
return () => subscription.remove();
}, []);
return (
<View style={{ flex: 1 }}>
{/* Main app content */}
<Text>Your App Content</Text>
{/* Conditional Blur View */}
{isBlurred && (
<View style={[StyleSheet.absoluteFill, { backgroundColor: 'white' }]}>
<Text style={{ color: 'black', textAlign: 'center', marginTop: '50%' }}>
App in Background
</Text>
</View>
)}
</View>
);
};
export default App;
Unfortunately, this approach only works for a brief moment and flickers before dismissing the view, so it’s not consistent when switching to the app switcher.
Is there a reliable way to block the app screen from being visible in the Android app switcher without using FLAG_SECURE
to disable screenshots and screen recordings while the app is in the foreground? I've seen apps like WhatsApp achieve this behavior, where they block content in the app switcher but still allow screenshots and recordings in the foreground when users turn on the App lock feature.
I found the solution to this issue after some time, so I'm sharing it here in case it helps others. Below is the code snippet in MainActivity.kt. For API level 33 and above, I'm using setRecentsScreenshotEnabled to avoid the system from taking a screenshot of the activity to be used as a representation in recents screen.
Reference: https://developer.android.com/reference/android/app/Activity#setRecentsScreenshotEnabled(boolean)
override fun onCreate(savedInstanceState: Bundle?) {
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
setRecentsScreenshotEnabled(false);
}
super.onCreate(null)
}