Copyright Notice:

This account published articles are from the public account, Chengxiang Moying (cxmyDev), chengxiang Moying all rights reserved.

Shall not be reproduced without permission.

One, foreword

For an Activity, it has a fixed life cycle, but it is just a management of the Activity itself. If there is a need to manage the life cycle of all activities in the App, it needs to be dealt with separately.

This time, you can use the Android level 14 (Android 4.0) the newly added API, AndroidLifecycleCallbacks can all Activity within the current process for unified management.

What is AndroidLifecycleCallback

AndroidLifecycleCallback is a new API for Android 4.0. It is a public interface within the Application. To use it, you also need to use it with the Application object.

Since AndroidLifecycleCallbacks is a global management Activity of the life cycle, it naturally need to correspond to the Activity of all life cycle method.

As you can see, AndroidLifecycleCallback here is an interface that we need to implement. And through registerActivityLifecycleCallbacks () method, it is registered to the current Application object, at the time of the need for, Also need to call unregisterActivityLifecycleCallbacks () solution.

From registered reconciliation of injection method can see that it is actually through an ArrayList array, mActivityLifecycleCallbacks maintained, that is to say, We can add multiple Callbacks listeners on the Application object.

When it is used depends entirely on Application methods, such as listening on activity.oncreate ().

Just take the Callbacks out and loop through the corresponding methods.

Three, the application scenario is more important than how to use

In fact, AndroidLifecycleCallback’s API is very simple and not complicated to use, so it is more important to use it in what scenario than how to use it.

Some common usage scenarios:

1. Add statistics to the page

If you need for each page, adding pages of PV, UV and other statistics, in addition to the corresponding declaration for each Activity within the cycle method, add the statistics, you can use AndroidLifecycleCallbacks undertake unity to add, facilitate management.

2. Exit all pages with one click

Since you can manage all activities, you can exit all activities with one click.

3. Determine whether the current App is in the foreground

For an App that exits to the background, one Activity must go onPause() and almost simultaneously no other Activity goes onResume(). The simplest way to do this is to use a Handler to delay onActivityPaused() and remove the delay message if another Activity immediately reaches onActivityResume(). If not, it is considered to have exited to the background.

What are the defects of LifecycleCallback

Actually ActivityLifecycleCallbacks as a whole, is very convenient and practical. However, it does have some drawbacks, such as Api limitations, inability to listen to the Fragment’s life cycle, etc.

Here is a simple solution to these problems, so I won’t show you any examples.

Since it needs to be compatible with devices below Android 4.0, and this is only used to listen for the Activity’s declaration cycle, we can actually use an Activity parent class and have all activities in the project inherit from it. In this case, Distribute the corresponding listening callback in the declaration cycle method of the parent class. The same goes for listening on the Fragment lifecycle.

If all this is a bit cumbersome to implement, see an open source project:

Github.com/soarcn/Andr…

In fact, its idea and the idea mentioned above is the same, but the package is more convenient to use, there is a need to read the source code is clear.

Five, the summary

Can monitor the state of all activities, is a very meaningful thing, it can achieve what functions, according to the actual needs and play our imagination.