2

I have situation where I need to prevent screenshots when app moves to background or in recent list and also when app goes to foreground I need to allow user to take screenshots.

Solutions I have tried in MainActivity.java.

  1. By using FLAG_SECURE in onPause and onResume like below

    public void onPause() {
            this.getWindow().setFlags(LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
            super.onPause();
        }
        public void onResume() {
            super.onResume();
            this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
        }
    
  2. Adding separate layout to show/hide in OnPause and OnResume

But none of them work. Kindly anyone help me on this one either Cordova or Android code.

Updated:

After spending so many hours, tried with different solutions and referred to multiple Cordova plugins finally able to resolve this issue using below code.

Instead of setting FLAG_SECURE in onResume and onPause. It can be used like below code.

`@Override
 public void onWindowFocusChanged(boolean hasFocus) {

  isWindowFocused = hasFocus;

  if(hasFocus) {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
    } else {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
                WindowManager.LayoutParams.FLAG_SECURE);
    }

  if (isBackPressed && !hasFocus) {
   isBackPressed = false;
   isWindowFocused = true;
  }

  super.onWindowFocusChanged(hasFocus);
 }`

I hope it helps for someone.

0 Answers0