Akhil Bodhade
Akhil Bodhade

Reputation: 51

How to disable and enable screenshot in Android while backgrounding the app

I want to disable screenshot when my app goes in the background or in-app switcher mode. Tried below solution but it does not work

public override fun onPause() {
   window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
   super.onPause()
}

public override fun onResume() {
   super.onResume()
   window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}

I tested on different devices it does not work on few devices like Nokia and Samsung and One plus and worked on MI device and in emulator above API level 24.

Upvotes: 2

Views: 4744

Answers (3)

Potass
Potass

Reputation: 267

Pasting my solution in case anybody is interested, as using onPause() and onResume() was not working in all scenarios:

import android.view.WindowManager.LayoutParams.FLAG_SECURE

override fun onTopResumedActivityChanged(isTopResumedActivity: Boolean) {
    if (isTopResumedActivity.not()) window.setFlags(FLAG_SECURE, FLAG_SECURE)

    super.onTopResumedActivityChanged(isTopResumedActivity)

    if (isTopResumedActivity) window.clearFlags(FLAG_SECURE)
}

Tested just on Pixel 6a.

Upvotes: 0

Anil Gangwar
Anil Gangwar

Reputation: 11

In API level 33

setRecentsScreenshotEnabled (boolean enabled) in onCreate of your main activity.

If set to false, this indicates to the system that it should never take a screenshot of the activity to be used as a representation in recents screen. By default, this value is true.

Upvotes: 1

ali riahi
ali riahi

Reputation: 126

use this code

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

Upvotes: 2

Related Questions