marcpetitvecino
marcpetitvecino

Reputation: 197

Issue extending BaseActivity() (Kotlin)

I'm developing an app and I need help with a strange problem.

I want to restrict the user from taking screenshots and stuff, and I know I have to use this line of code:

override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
    }

I have been told to put this on the onCreate() function of the BaseActivity() class, but it doesn't work.

All my activities extend BaseActivity(), BaseActivity extends AppCompatActivity()

The thing is, if I put the line of code directly on the onCreate() function of an activity, it works, but if I put it in BaseActivity() and extend it it doesn't. So, what am I doing wrong?

I have been told to put it like this

 abstract class BaseActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
        super.onCreate(savedInstanceState, persistentState)
    }
}

But it doesn't work either with the flags over or under the super.Oncreate

EDIT:

I'm talking to my senior and he says it's because I am overriding onCreate() on each Activity I have, and it eats the BaseActivity inherited onCreate, so, what can I do?

Upvotes: 0

Views: 493

Answers (2)

marcpetitvecino
marcpetitvecino

Reputation: 197

I solved it by doing this:

 abstract class BaseActivity : AppCompatActivity() {
     fun safetyOnCreate() =  window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
 }

And then on the activity just super.safetyOnCreate() in the onCreate() function

Upvotes: 1

AbhayBohra
AbhayBohra

Reputation: 2117

Put these flags above super.onCreate statement like this in your base activity

window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
super.onCreate(savedInstanceState, persistentState)

Upvotes: 1

Related Questions