在开发中由于业务的需求,我们需要在Activity对应的生命周期方法中去完成对应的工作,比如说需要在Activity的onResume中注册某个SDK或者广播接收器、抑或是尝试打开Camera、判断app 是否在后台运行除了令人蛋疼的判断当前runningProcess 或者runningTasks 方法等,然后在Activity的onPause()中及时注销或释放掉,以前也许会在每个Activity对应的方法中增加对应的代码即可,但是从一定意义上说这代码是冗余的且不优雅,从Android4.0之后添加了Application.ActivityLifecycleCallbacks接口,我们开发者就有了一种更优雅的方案。
记得我第一次知道这个接口之后,第一时间跑到官网中查询文档,结果发现Google 又偷懒了,Summary部分竟然一句话都没有关于它的描述,差点怀疑是否进错了网站,ORZ,才发现其实是这个接口太简单了背后的原理也十分简单,本来我还以为会是观察者模式,后面发现就是一个普通回调。但是借助它我可以优雅地监控Activity的生命周期——一经注册,就会自动监听整个APP 中所有Activity 的生命周期方法的执行(会在对应的父类Activity的方法被执行之后自动触发)。
Application.ActivityLifecycleCallbacks是Application中的一个接口,使用起来也很简单,只需要调用registerActivityLifecycleCallbacks方法即可完成注册(通常是在Application中完成的)。
public class Application extends ContextWrapper implements ComponentCallbacks2 {
private ArrayList mComponentCallbacks =
new ArrayList();
private ArrayList mActivityLifecycleCallbacks =
new ArrayList();
private ArrayList mAssistCallbacks = null;
/** @hide */
public LoadedApk mLoadedApk;
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 Application() {
super(null);
}
@CallSuper
public void onCreate() {
}
/**
* This method is for use in emulated process environments. It will
* never be called on a production Android device, where processes are
* removed by simply killing them; no user code (including this callback)
* is executed when doing so.
*/
@CallSuper
public void onTerminate() {
}
@CallSuper
public void onConfigurationChanged(Configuration newConfig) {
Object[] callbacks = collectComponentCallbacks();
if (callbacks != null) {
for (int i=0; i@CallSuper
public void onLowMemory() {
Object[] callbacks = collectComponentCallbacks();
if (callbacks != null) {
for (int i=0; i@CallSuper
public void onTrimMemory(int level) {
Object[] callbacks = collectComponentCallbacks();
if (callbacks != null) {
for (int i=0; iif (c instanceof ComponentCallbacks2) {
((ComponentCallbacks2)c).onTrimMemory(level);
}
}
}
}
/*
**注册ActivityLifecycleCallbacks回调
*/
public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
synchronized (mActivityLifecycleCallbacks) {
mActivityLifecycleCallbacks.add(callback);
}
}
public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
synchronized (mActivityLifecycleCallbacks) {
mActivityLifecycleCallbacks.remove(callback);
}
}
// ------------------ Internal API ------------------
/* package */ void dispatchActivityCreated(Activity activity, Bundle savedInstanceState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i/* package */ void dispatchActivityStarted(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i/* package */ void dispatchActivityResumed(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i/* package */ void dispatchActivityPaused(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i/* package */ void dispatchActivityStopped(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i/* package */ void dispatchActivitySaveInstanceState(Activity activity, Bundle outState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i/* package */ void dispatchActivityDestroyed(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i
然后在Activity的对应的生命周期方法的内部,通过Application来调用对应的dispatchActivityXxxxx方法,从而实现全生命周期的监听
public class Activity extends ContextThemeWrapper implements LayoutInflater.Factory2,Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback, WindowControllerCallback {
...
@MainThread
@CallSuper
protected void onCreate(@Nullable Bundle savedInstanceState) {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
if (mLastNonConfigurationInstances != null) {
mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
}
if (mActivityInfo.parentActivityName != null) {
if (mActionBar == null) {
mEnableDefaultActionBarUp = true;
} else {
mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
}
}
if (savedInstanceState != null) {
Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
? mLastNonConfigurationInstances.fragments : null);
}
mFragments.dispatchCreate();
getApplication().dispatchActivityCreated(this, savedInstanceState);
if (mVoiceInteractor != null) {
mVoiceInteractor.attachActivity(this);
}
mCalled = true;
}
@CallSuper
protected void onStart() {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
mCalled = true;
mFragments.doLoaderStart();
getApplication().dispatchActivityStarted(this);
}
...
}
Application.ActivityLifecycleCallbacks中对应的监听的生命周期方法会在Activity中的生命方法调用父类的方法之后被触发,由源码也可得知。
package com.crazyview.activitylifecycle;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;
/**
* Created by cmo on 2017/12/23.
*/
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());
}
});
}
}
package com.crazyview.activitylifecycle;
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();
}
}
打开MainActivity时候的运行结果:
finish当前Activity时:
adb shell dumpsys activity | findstr "mFocusedActivity"
adb logcat -s tag >d:log.txt
adb logcat -v time >a.log
public static String getTopActivity(Context context) {
android.app.ActivityManager am = (android.app.ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
return cn.getShortClassName().toString();
}