在Application
中有一个registerActivityLifecycleCallbacks
方法,实现注册Activity生命周期回调,首先看下在Application源码中实现:
private ArrayList<ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
new ArrayList<ActivityLifecycleCallbacks>();
public interface ActivityLifecycleCallbacks {
void onActivityCreated(Activity activity, Bundle savedInstanceState);
void onActivityStarted(Activity activity);
void onActivityResumed(Activity activity);
void onActivityPaused(Activity activity);
void onActivityStopped(Activity activity);
void onActivitySaveInstanceState(Activity activity, Bundle outState);
void onActivityDestroyed(Activity activity);
}
public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
synchronized (mActivityLifecycleCallbacks) {
mActivityLifecycleCallbacks.add(callback);
}
}
public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
synchronized (mActivityLifecycleCallbacks) {
mActivityLifecycleCallbacks.remove(callback);
}
}
代码很简单,一个ArrayList保存所有注册回调接口实现对象,ActivityLifecycleCallbacks
声明和Activity生命周期相关的方法,那在哪里调用这些方法呢?接着再看下Application中的源码,发现如下方法:
/* package */ void dispatchActivityCreated(Activity activity, Bundle savedInstanceState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityCreated(activity,
savedInstanceState);
}
}
}
/* package */ void dispatchActivityStarted(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityStarted(activity);
}
}
}
/* package */ void dispatchActivityResumed(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityResumed(activity);
}
}
}
/* package */ void dispatchActivityPaused(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityPaused(activity);
}
}
}
/* package */ void dispatchActivityStopped(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityStopped(activity);
}
}
}
/* package */ void dispatchActivitySaveInstanceState(Activity activity, Bundle outState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivitySaveInstanceState(activity,
outState);
}
}
}
/* package */ void dispatchActivityDestroyed(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityDestroyed(activity);
}
}
}
各种dispatch相关的方法,这里是回调ArrayList接口实现对象的方法,那哪里调用这些方法了,等等,我们想下,既然在Activity生命周期会回调这些方法,那么Activity中应该存在调用的逻辑?好的,那我们看下Activity中各生命周期的方法:
//关键代码 这里我们看下onStart方法
@CallSuper
protected void onStart() {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
mCalled = true;
mFragments.doLoaderStart();
getApplication().dispatchActivityStarted(this);//这不是调用dispatch相关的方法,哈哈
if (mAutoFillResetNeeded) {
AutofillManager afm = getAutofillManager();
if (afm != null) {
afm.onVisibleForAutofill();
}
}
}
可以看到在onStart
生命周期方法中调用了getApplication().dispatchxxxx方法,同理,如果大家再去看的话可以看到其它相关生命周期方法的回调,这里就不一一贴上源码了。到这里,我们就知道了调用逻辑。那注册生命周期回调registerActivityLifecycleCallbacks
有什么用呢?
这里说个特殊需求的实现:实现在断网情况下显示一个弹窗提示用户?这个需求想必有很多同鞋遇到的,这里我们不再讨论通过广播来监听网络状态的改变,我们只讨论如何来显示弹窗,大部分做法会在BaseActivity中写上相关弹窗的代码,
这里提供另外一种思路,就是注册registerActivityLifecycleCallbacks
回调,可以得到Activity栈顶的activity来显示相关的弹窗。有兴趣的同鞋可以试下哈。