Android - ActivityLifecycleCallbacks application life cycle centralized management

Official address: https://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks

 Introduction

 In the Application class after Android API 14, we provide us with a registration method for application life cycle callbacks to centrally manage the application life cycle. This interface is called registerActivityLifecycleCallbacks, through which you can register your own ActivityLifeCycleCallback, each Activity The life cycle will call back to the corresponding method here.

With the ActivityLifeCycleCallback interface, we can complete the related requirements such as limiting the number of activities that we wanted to do before, because the life cycle of all activities will be called back here, and we can handle it according to the conditions.

Using ActivityLifecycleCallbacks we can complete functions similar to the following:

1. Limit the number of specified activities

2. Control only one Activity to be opened under certain circumstances

3. Determine the status of the front and back of the App

//Application.ActivityLifecycleCallbacks是Application中的一个接口,使用起来也很简单,只需要调用registerActivityLifecycleCallbacks方法即可完成注册。Application.ActivityLifecycleCallbacks中对应的监听的生命周期方法会在Activity中的生命方法调用父类的方法之后被触发。

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        inivV();
    }

    private void inivV() {
        this.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
                Log.e(
                        "Lifecycle", activity.getLocalClassName() + " was Created" + "activity==null   "
                                + (activity == null) + "     activity.isFinishing()  " + (activity.isFinishing()) + " " +
                                "   activity.isDestroyed()  " + activity.isDestroyed()
                );
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                Log.e(
                        "Lifecycle", activity.getLocalClassName() + " was Created" + "activity==null   "
                                + (activity == null) + "     activity.isFinishing()  " + (activity.isFinishing()) + " " +
                                "   activity.isDestroyed()  " + activity.isDestroyed()
                );

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {

                Log.e(
                        "Lifecycle", activity.getLocalClassName() + " was Created" + "activity==null   "
                                + (activity == null) + "     activity.isFinishing()  " + (activity.isFinishing()) + " " +
                                "   activity.isDestroyed()  " + activity.isDestroyed()
                );
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
                Log.e(
                        "Lifecycle", activity.getLocalClassName() + " was Created" + "activity==null   "
                                + (activity == null) + "     activity.isFinishing()  " + (activity.isFinishing()) + " " +
                                "   activity.isDestroyed()  " + activity.isDestroyed()
                );

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                Log.e(
                        "Lifecycle", activity.getLocalClassName() + " was Created" + "activity==null   "
                                + (activity == null) + "     activity.isFinishing()  " + (activity.isFinishing()) + " " +
                                "   activity.isDestroyed()  " + activity.isDestroyed()
                );

            }

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

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

                Log.e(
                        "Lifecycle", activity.getLocalClassName() + " was Created" + "activity==null   "
                                + (activity == null) + "     activity.isFinishing()  " + (activity.isFinishing()) + " " +
                                "   activity.isDestroyed()  " + activity.isDestroyed()
                );
            }
        });
    }
}

 

Guess you like

Origin blog.csdn.net/m0_59482482/article/details/129552564