ProcessLifecycleOwner judges the front and back switching of the Android application

Android App at this point in 2019

I have used a method to switch between the front and back of the Android application. The method is to call registerActivityLifecycleCallbacks in the Application class, and then use a global variable to count, increase the counter in onActivityStarted(), and decrease the counter in the onActivityStopped method. One. And judge the value of the counter in the onActivityStarted and onActivityStopped methods respectively. If the value of the counter in onActivityStarted is 0, it proves to enter the foreground from the background, and if the counter in onActivityStopped is equal to 0, it proves to enter the background from the foreground.

The main part of the code is as follows:

registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
              
            }

            @Override
            public void onActivityStarted(Activity activity) {
                if (sForegroundCount == 0) {
                    // 进入foreground
               
                }
                sForegroundCount++;

          
            }

            @Override
            public void onActivityResumed(Activity activity) {
         
            }

            @Override
            public void onActivityPaused(Activity activity) {
              
            }

            @Override
            public void onActivityStopped(Activity activity) {
                sForegroundCount--;
                if (sForegroundCount == 0) {
                    // 进入background

                 
                }

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
         
            }
        });

This is indeed possible to achieve the goal, but to be honest, the way to achieve it is very awkward.

The emergence of ProcessLifecycleOwner overcomes this problem in one fell swoop, making the front-end and back-end switching of the monitoring program elegant. This class is a class in the google Lifecycle framework. Use need to introduce

    implementation 'android.arch.lifecycle:extensions:1.1.1'
    implementation 'android.arch.lifecycle:runtime:1.1.1'

It's very simple to use:

First define a class that implements the LifecyclerObserver interface:

public class LifecyclerChecker implements LifecycleObserver

Then add the following code to this class:

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    private void onAppBackground() {
        // 应用进入后台
        Log.e("test","LifecycleChecker onAppBackground ON_STOP");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    private void onAppForeground() {
        // 应用进入前台
        Log.e("test","LifecycleChecker onAppForeground ON_START");

    }

Of course, the above method names are arbitrary. The important thing is to annotate @OnLifecycleEvent(Lifecycle.Event.ON_STOP) and @OnLifecycleEvent(Lifecycle.Event.ON_START). The first one means that the application enters the background, and the second one means that the application enters the foreground. .

Finally, call the following method at the appropriate place:

      ProcessLifecycleOwner.get().getLifecycle().addObserver(new LifecycleChecker());

The following method can be called either in the onCreate of the Application or in the onCreate of the Activity or other life cycle functions. You can call it wherever you like.

As long as the above code is called, you can call back the corresponding method when the application switches between front and back.

Guess you like

Origin blog.csdn.net/awy1988/article/details/99431616