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.
Requirement:
- When the app is in the foreground, the user should be able to take screenshots and record the screen without any restrictions.
- When the app is moved to the background or viewed in the app switcher, the screen should be blocked to prevent third parties from viewing sensitive content.
What I’ve Tried:
- Using
FLAG_SECURE
inonPause()
andonResume()
:
- I tried turning on
FLAG_SECURE
in theonPause()
method and turning it off inonResume()
. 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.
- Setting
FLAG_SECURE
inonCreate()
:
- I also tried setting
FLAG_SECURE
inonCreate()
, 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);
}
- Using React Native and App State:
- I also tried implementing this in React Native by checking the app state and rendering a blank screen (or a blur screen) when the app goes to the background. I used AppState to track the app lifecycle and show a view on top of the current screen when the app moves to the background.
However, this approach results in a flickering effect — the view briefly shows up but quickly dismisses, making it unreliable when the app is sent to the background.
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.
Question:
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.
Set flags before super.onPause()
. For more information on the proper order of calling superclass methods in Android activities, please refer to this resource.
@Override
protected void onPause() {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
super.onPause();
}
1