Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
On Android 12, if I create an simple app with
WindowManager.LayoutParams.FLAG_SECURE
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.setFlags(
WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE
setContentView(R.layout.activity_main)
the window does not secure if I try to switch apps
however, if I switch back, the window does secure
In both cases, switching to or from, the window secures on Android 11, but not on Android 12. Any idea why?
As of Android version 12, it was the desired behavior. additionally, you can check the Google Issue Tracker.
https://issuetracker.google.com/issues/237190495
Instead of onPause and onResume, we can use onWindowFocusChanged(boolean).when the activity does not have focus we can enable FLAG_SECURE when the app does regain focus we can clear FLAG_SECURE
Easy approach using View#onWindowFocusChanged(boolean)
on your Activity:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
// allow screenshots when activity is focused
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
} else {
// hide information (blank view) on app switcher
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.