The most straight-forward and compatible way to hide the app contents from the recents screen is by using FLAG_SECURE
, which hides the app contents from the recents screen, stops the screen from being recorded, and stops screenshots from being taken, among others. I'd like to only hide the app contents from the recents screen while allowing all other actions.
Other answers here suggest setting the flag in onPause()
and removing it in onResume()
, both of the activity. This works fine on most phones, but on some phones such as my OnePlus on Android 9 and my coworkers Moto G on Android 10, the manufacturer has implemented custom navigation gestures aside from the standard options of 3 bottom buttons and the pill navigation. When using these custom forms of navigation, the app does call onPause()
but setting FLAG_SECURE
does not hide the app contents on the recents screen.
Here is my code:
override fun onPause() {
super.onPause()
// Set secure flag so the multitasking screen doesn't show the app contents
// Need to clear on resume because this also stops screenshots from being taken
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
}
override fun onResume() {
super.onResume()
// Clear secure flag so the app is able to take screenshots
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
I have also tried setting the flag before calling super
to no avail, as in the following code:
override fun onPause() {
// Set secure flag so the multitasking screen doesn't show the app contents
// Need to clear on resume because this also stops screenshots from being taken
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
super.onPause()
}
override fun onResume() {
// Clear secure flag so the app is able to take screenshots
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
super.onResume()
}
I would be inclined to believe that this is an error of not properly handling state changes for custom navigation on the manufacturer's part, but I found that the TD Bank app (no login necessary) does in fact work as expected, with screenshots being allowed and the OS properly hiding the app contents when put into background.
Any ideas?