How to block app screen in the Android app switcher without disabling screenshots and screen recordings in the foreground?

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:

  1. Using FLAG_SECURE in onPause() and onResume():
  • I tried turning on 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.

  1. Setting FLAG_SECURE in onCreate():
  • I also tried setting 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);
}
  1. 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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật