1

I couldn't find anything about this kind of feature online. So basically what I'm trying to achieve is when the user clicks Home button (in other words onPause() method called) I want to make the activity invisible or blurry on back stack for maximum security.

here is what I meant

I want this for not just one activity but every activity, what's the easiest way to achieve this?

Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85

3 Answers3

0

Gonna go risky here with this answer since i am not 100% that android handles the background stack this way, but i believe that background "image" of activity is created as a screenshot of the app going to the background after onPause is called. So basically all you should do is blur the current state of your app before it exits to the background and the background image should be blurred. Or, if blurring all elements is too heavy or unacceptable in any way, perhaps you could take a ss of your app, show it in modal(fullscreen modal) and blur the ss itself.

Here are some advanced blurring techniques: http://trickyandroid.com/advanced-blurring-techniques/

Or as answered here (bitmap blur)

Alen Zarkovic
  • 224
  • 2
  • 11
  • 1
    Test this method by using a plain color foreground first, which is fast. Then delve into something comparably slower and more complex such as blurring images. – Eugen Pechanec Dec 11 '17 at 14:30
0

You could try to put a View on top of everything on your Activity, and listen to onPause() to toggle its visibility. You could make this view have a solid color background with the app logo on the center, for example.

Make it visible every time the app is paused, and hide it on onResume(). I think this could mimic what Paypal and some other apps do.

Mauker
  • 11,237
  • 7
  • 58
  • 76
  • This is not a proper solution, when the app calls onPause, background becomes visible for a second then again you see the Activity's regular view on background. – Ege Kuzubasioglu Dec 18 '17 at 07:27
0

First of all, calling lifecycle methods doesn't work since they are called when the activity changes and so on.

So this is how I resolved this:

I created a class that extends from LifecycleObserver (this is pretty new)

class ForegroundBackgroundListener : LifecycleObserver {

@OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun appStarted() {
        Timber.d("App is on foreground")
       //Todo visibility: gone
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun appStopped() {
        Timber.d("App is on background")
        //Todo visibility: visible
}

And then in my Application class:

class ForegroundBackgroundApp : Application() {

    private lateinit var appObserver: ForegroundBackgroundListener

        override fun onCreate() {
            super.onCreate()

            ProcessLifecycleOwner.get()
                    .lifecycle
                    .addObserver(
                            ForegroundBackgroundListener()
                                    .also { appObserver = it })
        }
    }

As you see I can detect foreground/background changes and do something accordingly.

Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85