1

As per the requirement, we have to hide the App's last stored state/snap when viewed from app carousel in minimized state. Now in the app, I'm using JetPack's lifecycle events - ON_START and ON_STOP in order to detect the state if the app going in background or coming to foreground. Here is the code snippet for reference -

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        public void onAppBackgrounded() {
            //App in background
           getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE))
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        public void onAppForegrounded() {
            // App in foreground
         getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE)) 
        }

So basically what I'm trying to do here is - I'm enabling flag secure when app goes to background and disabling it when it comes back to foreground in order to allow app to take screenshots. As by using flag secure, Android by default will disable the user to take app screenshots.

This is working perfectly with onPause and onResume. Is there any way where we can make it work using Lifecycle events? Thanks.

Shraddha B
  • 21
  • 7
  • To me it looks like you have `setFlags` and `clearFlags` reversed wrt. your intention. – laalto Jan 24 '22 at 13:11
  • @laalto Pls see the edited latest version. Thanks. – Shraddha B Jan 24 '22 at 13:15
  • 1
    "This is working perfectly with onPause and onResume" -- unless you have tested this on lots and lots of device models, I would not count on that. AFAIK, the only reliable recipe for using `FLAG_SECURE` is to set it once, in `onCreate()` of the activity, before calling `setContentView()` or otherwise populating the UI. – CommonsWare Jan 24 '22 at 14:01

1 Answers1

0

the same experience, works if you call

getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE))

in onCreate() of the activity, before calling "setContent"

Peter
  • 260
  • 5
  • 3