title: How do I hide app contents on the recents screen while allowing screenshots on phones with non-standard navigation gestures?
body: <p>The most straight-forward and compatible way to hide the app contents from the recents screen is by using <code>FLAG_SECURE</code>, 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.</p>
  <p>Other answers <a href="https://stackoverflow.com/a/49442984/8217881">here</a> suggest setting the flag in <code>onPause()</code> and removing it in <code>onResume()</code>, 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 <code>onPause()</code> but setting <code>FLAG_SECURE</code> does not hide the app contents on the recents screen.</p>
  <p>Here is my code:</p>
  <pre><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)
      }
  
  </code></pre>
  <p>I have also tried setting the flag before calling <code>super</code> to no avail, as in the following code:</p>
  <pre><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()
      }
  </code></pre>
  <p>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 <a href="https://play.google.com/store/apps/details?id=com.tdbank" rel="noreferrer">TD Bank app</a> (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.</p>
  <p>Any ideas?</p>