赞
踩
在 Android API 14 之后的 Application 类中,为我们提供了一个应用生命周期回调的注册方法,用来对应用的生命周期进行集中管理,这个接口叫 registerActivityLifecycleCallbacks,可以通过它注册自己的ActivityLifeCycleCallback,每一个 Activity 的生命周期都会回调到这里的对应方法。
Activity 生命周期如下图(摘自谷歌开发者官网)
ActivityLifecycleCallbacks 接口文档可以访问:ActivityLifecycleCallbacks 接口文档
通过对比发现,两者几乎是一一对应的,不管是做 Activity 的限制还是 Activity 的状态统计都是非常方便的,而且里面还有一个 void onActivitySaveInstanceState(Activity activity, Bundle outState) 方法,非常方便我们来保存 Activity 状态数据。
有了 ActivityLifeCycleCallback 接口,我们就可以完成之前我们想做的类似限制制定 Activity 个数等相关需求,因为所有Activity的生命周期都会在这里回调,我们可以根据条件随心处理。
使用 ActivityLifecycleCallbacks 我们可以完成类似如下的功能:
限制指定的 Activity 的数量
控制在特定情况下只会有一个 Activity 被打开
判断 App 前后台状态
Application.ActivityLifecycleCallbacks 是 Application 中的一个接口,使用起来也很简单,只需要调用 registerActivityLifecycleCallbacks 方法即可完成注册。Application.ActivityLifecycleCallbacks 中对应的监听的生命周期方法会在 Activity 中的生命方法调用父类的方法之后被触发。
import android.app.Activity; import android.app.Application; import android.os.Bundle; import android.util.Log; public class LifecycleApplication extends Application { @Override public void onCreate() { super.onCreate(); init(); } private void init() { registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.e("Lifecycle",activity.getLocalClassName()+" was Created"+"activity==null " +(activity==null)+" activity.isFinishing() "+(activity.isFinishing())+" activity.isDestroyed() "+activity.isDestroyed()); } @Override public void onActivityStarted(Activity activity) { Log.e("Lifecycle",activity.getLocalClassName()+" was Started"+"activity==null " +(activity==null)+" activity.isFinishing() "+(activity.isFinishing())+" activity.isDestroyed() "+activity.isDestroyed()); } @Override public void onActivityResumed(Activity activity) { Log.e("Lifecycle",activity.getLocalClassName()+" was oResumed"+"activity==null " +(activity==null)+"activity.isFinishing() "+(activity.isFinishing())+"activity.isDestroyed() "+activity.isDestroyed()); } @Override public void onActivityPaused(Activity activity) { Log.e("Lifecycle",activity.getLocalClassName()+" was Pauseed"+"activity==null " +(activity==null)+"activity.isFinishing() "+(activity.isFinishing())+"activity.isDestroyed() "+activity.isDestroyed()); } @Override public void onActivityStopped(Activity activity) { Log.e("Lifecycle",activity.getLocalClassName()+" was Stoped"+"activity==null " +(activity==null)+"activity.isFinishing() "+(activity.isFinishing())+"activity.isDestroyed() "+activity.isDestroyed()); } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.e("Lifecycle",activity.getLocalClassName()+" was SaveInstanceState"+"activity==null " +(activity==null)+"activity.isFinishing() "+(activity.isFinishing())+"activity.isDestroyed() "+activity.isDestroyed()); } @Override public void onActivityDestroyed(Activity activity) { Log.e("Lifecycle",activity.getLocalClassName()+" was Destroyed"+"activity==null" +(activity==null)+" activity.isFinishing() "+(activity.isFinishing())+" activity.isDestroyed()"+activity.isDestroyed()); } }); } }
在清单中声明 Application,无需在 Activity 添加额外的代码就可以实现监控:
import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; public class MainActivity extends AppCompatActivity { public static final String LIFECYCLE = "MainActivity:Lifecycle"; @Override protected void onCreate(Bundle savedInstanceState) { Log.e(LIFECYCLE, "onCreate() is Running__before super.onCreate called"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e(LIFECYCLE, "onCreate() is Running__after super.onCreate called"); } @Override protected void onRestart() { Log.e(LIFECYCLE, "onRestart() is Running__before super's called"); super.onRestart(); Log.e(LIFECYCLE, "onRestart() is Running__after super's called"); } @Override protected void onStart() { Log.e(LIFECYCLE, "onStart() is Running__before super.onStart called"); super.onStart(); Log.e(LIFECYCLE, "onStart() is Running__after super.onStart called"); } @Override protected void onResume() { Log.e(LIFECYCLE, "onResume() is Running__before super.onResume called"); super.onResume(); Log.e(LIFECYCLE, "onResume() is Running__after super.onResume called"); } @Override protected void onPause() { Log.e(LIFECYCLE, "onPause() is Running__before super's called"); super.onPause(); Log.e(LIFECYCLE, "onPause() is Running__after super's called"); } @Override protected void onStop() { Log.e(LIFECYCLE, "onStop() is Running__before super's called"); super.onStop(); Log.e(LIFECYCLE, "onStop() is Running__after super's called"); } @Override protected void onDestroy() { Log.e(LIFECYCLE, "onDestroy() is Running__before super's called"); super.onDestroy(); Log.e(LIFECYCLE, "onDestroy() is Running__after super's called"); } public void toTask(View view) { startActivity(new Intent(this, TaskActivity.class)); //finish(); } }
App 前后台的切换一般情况下都是按 Home 来进行,当然也有别的方式,但是此时 Activity 的生命周期是一样的:
HOME 键前后台切换 Activity 的执行顺序:onPause->onStop->onRestart->onStart->onResume
BACK 键前后台切换 Activity 键的顺序: onPause->onStop->onDestroy->onCreate->onStart->onResume
其实按 BACK 按键就是退出 app 了,不算是前台后切换。现在我们知道 App 的由前台切换到后台所有打开的 Activity 会走:
onPause->onStop ,后台切换到前台所有打开的 Activity 会走:->onStart->onResume。前后台切换 App 所有打开的Activity 的生命周期都是一样的,这样我就可以在 ActivityLifecycleCallbacks 回调接口中记录生命周期。
以前做过所有的 activity 都集成 baseactivity,重写 onCreate方法,onStop 方法,定义一个变量 count 统计打开的 activity数,onCreate 时 count++,onStop 时 count–,当 count == 0 时,应用就在后台了。现在,从api14 开始,api 提供了一个所有 activity 生命周期的回调,就是 ActivityLifecycleCallbacks,每个 activity 的生命周期都会触发,通过registerActivityLifecycleCallbacks 注册这个回调。
其实和重写 baseactivity 的 onCreate 和 onStop 一样的,在回调的 onResume 中 count++,onStop 中 count–,在 onStop 中增加一个个判断,if(count == 0){“我在后台了”},为什么在 onStop 中呢?因为根据上面的分析,不论是 back 返回还是 home 退出,还是其他手段退出,activity 总会回调 onStop 的。
ActivityLifecycleCallbacks 是 Application 中的一个接口,既然是在 Application 中,不得不说说 Application 类,别急,一个个来说道说道。
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);
}
有很多方法声明,仔细一看,很熟悉啊,没错,这些方法与 Activity 的整个生命周期相关,而且,通过命名,可以大体猜测,这些方法会在 Activity 周期方法调用完后被执行。接下来,看看 Application 中还有哪儿些与 ActivityLifecycleCallbacks 相关的,代码如下:
public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
synchronized (mActivityLifecycleCallbacks) {
mActivityLifecycleCallbacks.add(callback);
}
}
public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
synchronized (mActivityLifecycleCallbacks) {
mActivityLifecycleCallbacks.remove(callback);
}
}
这里,两个方法,一个注册 ActivityLifecycleCallbacks,一个反注册 ActivityLifecycleCallbacks,还有一个mActivityLifecycleCallbacks,可以看出是个成员变量。在 Application 中定义 mActivityLifecycleCallbacks,代码如下:
private ArrayList<ActivityLifecycleCallbacks> mActivityLifecycleCallbacks = new ArrayList<ActivityLifecycleCallbacks>();
继续阅读,会发现如下方法:
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); } } } void dispatchActivityStarted(Activity activity) { Object[] callbacks = collectActivityLifecycleCallbacks(); if (callbacks != null) { for (int i=0; i<callbacks.length; i++) { ((ActivityLifecycleCallbacks)callbacks[i]).onActivityStarted(activity); } } } void dispatchActivityResumed(Activity activity) { Object[] callbacks = collectActivityLifecycleCallbacks(); if (callbacks != null) { for (int i=0; i<callbacks.length; i++) { ((ActivityLifecycleCallbacks)callbacks[i]).onActivityResumed(activity); } } } void dispatchActivityPaused(Activity activity) { Object[] callbacks = collectActivityLifecycleCallbacks(); if (callbacks != null) { for (int i=0; i<callbacks.length; i++) { ((ActivityLifecycleCallbacks)callbacks[i]).onActivityPaused(activity); } } } void dispatchActivityStopped(Activity activity) { Object[] callbacks = collectActivityLifecycleCallbacks(); if (callbacks != null) { for (int i=0; i<callbacks.length; i++) { ((ActivityLifecycleCallbacks)callbacks[i]).onActivityStopped(activity); } } } 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); } } } void dispatchActivityDestroyed(Activity activity) { Object[] callbacks = collectActivityLifecycleCallbacks(); if (callbacks != null) { for (int i=0; i<callbacks.length; i++) { ((ActivityLifecycleCallbacks)callbacks[i]).onActivityDestroyed(activity); } } }
以上这些方法,都有个共同点,就是调用了 collectActivityLifecycleCallbacks 方法,而且遍历 callbacks,类型转换为ActivityLifecycleCallbacks,collectActivityLifecycleCallbacks 方法做了什么呢?查源码发现它会将 list 转化为数组,代码如下:
private Object[] collectActivityLifecycleCallbacks() {
Object[] callbacks = null;
synchronized (mActivityLifecycleCallbacks) {
if (mActivityLifecycleCallbacks.size() > 0) {
callbacks = mActivityLifecycleCallbacks.toArray();
}
}
return callbacks;
}
那么问题来了,Applicatipn 中的 dispatchActivityXXX 的这些方法,是谁调用呢?如果对 Activity 源码了解的话,很容易发现如下代码:
protected void onCreate(@Nullable Bundle savedInstanceState) { `//... getApplication().dispatchActivityCreated(this, savedInstanceState); } protected void onStart() { //... getApplication().dispatchActivityStarted(this); } protected void onResume() { //... getApplication().dispatchActivityResumed(this); //... }
onPause(),onStop(),onDestroy(),同样调用 getApplication().dispatchActivityPaused(this) 。
总结整理整个执行过程:、
需要定义一个类 A,实现 ActivityLifecycleCallbacks,并且实现接口方法 registerActivityLifecycleCallbacks 注册类 A 对象,
系统调用 Activity 的 attach 方法,绑定 Application 上下文。
系统调用 Activity 生命周期方法,相应的调用 Application 中的 dispatchActivityXXX 方法,dispatchActivityXXX 方法内部,回调 A 对象中方法的具体实现,从而完成,对 Activity 生命周期完整的跟踪监听。
其优点是:官方提供对 Activity 生命周期的统一监听,避免代码在某类中的显式调用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。