0

I want to get callback as soon as moves away from the app. This should include 2 cases.

  1. When user presses home button & the app is minimised.
  2. When user presses recent app button, where app is visible but has lost focus.

Before Android 12, I was able to achieve it via following ways by placing this code in application class

override fun onActivityResumed(activity: Activity) {
        activity.window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
    }

override fun onActivityPaused(activity: Activity) {
        activity.window.setFlags(
            WindowManager.LayoutParams.FLAG_SECURE,
            WindowManager.LayoutParams.FLAG_SECURE
        )
    }

But for devices running Android 12 & above, callback is not sent to onActivityPaused for case 2. And happens only for case 1
Another way for this to work is

override fun onWindowFocusChanged(hasFocus: Boolean) {
    super.onWindowFocusChanged(hasFocus)
    if (!hasFocus) {
        Timber.d("Ujjwal: Lost focus")
    }
}

But there are 2 issues:

  1. I will have to put in all activities or some BaseActivity
  2. There are cases when the flow goes to some 3rd party SDK which has their own activity launched, this workaround won't work in those scenarios.

Can anyone help here.
Main objective is whenever user moves out of the app, the app should not take screenshot & show that data in recent app screen due to security & sensitive data.
The implementation needs to be done in application class & should work for all Android versions.

0 Answers0