Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

Android: how to find out, if activity was started or resumed (comes to foreground) from outside of application

Consider following situation:

I have an application, which contains some critical information (e.g. banking app). The application itself has multiple screens.

I would like to have the following behavior: Every time after application becomes inactive for any reason (e.g.: user pressed "home" button, user switched to other app using list of recent tasks, user shut down display with power button, input call happened etc...)... so every time after that I want, that when user comes back to the app, the password is requested.

The first solution, which comes to mind is to request password every time in onResume(). But this of course does not work, because this method is also called, when internal transition from one activity to another within the application happens (and of course, at this point the password should not be requested).

So to solve this I would need to know, if onResume() was triggered from "internal transition" or from outside the application.

I would be very grateful, if someone could suggest, how to do it.

If there is another solution to the problem, I would also be happy to know it.

Thank you.

question from:https://stackoverflow.com/questions/65833428/android-how-to-find-out-if-activity-was-started-or-resumed-comes-to-foregroun

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think what you're looking for is ProcessLifecycleOwner

ProcessLifecycleOwner is a special kind of LifecycleOwner that is a composite of all of your Activities. It provides lifecycle for the whole application process

   implementation "androidx.lifecycle:lifecycle-runtime:2.0.0"
   implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
   annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0"

Create an Application class and set custom app lifecycle observer

class App : Application() {

    override fun onCreate() {
        super.onCreate()

        ProcessLifecycleOwner.get().lifecycle.addObserver(appLifecycleObserver)
    }
}

Custom LifecycleObserver class

class AppLifecycleObserver : LifecycleObserver {

    /**
     * When application come to Foreground
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onEnterForeground() {
        // do something
    }

    /**
     * When application goes to Background
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onEnterBackground() {
        // do something
    }

}

You check the documentation for detail explanation and this article.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...