Glide中一个重要特性是Request可以随Activity或Fragment的onStart而resume,onStop而pause,onDestroy而clear,从而节约流量和内存,并且防止内存泄露。在阅读Glide相关源码后,其核心使用观察者模式,引入项目后实现EventBus的自动注册、自动注销。
观察者接口public interface LifecycleListener {
/**
* 当Activity、Fragment执行onStart()方法时执行
*/
void onStart();
/**
* 当Activity、Fragment执行onStar()方法时执行
*/
void onStop();
/**
* 当Activity、Fragment执行onDestroy()方法时执行
*/
void onDestroy();
}
观察者管理接口
public interface Lifecycle {
/**
* 添加观察者
*/
void addListener(LifecycleListener listener);
}
观察者辅助管理类
public class ActivityFragmentLifecycle implements Lifecycle {
private final Set<LifecycleListener> lifecycleListeners =
Collections.newSetFromMap(new WeakHashMap<LifecycleListener, Boolean>());
private boolean isStarted;
private boolean isDestroyed;
@Override
public void addListener(LifecycleListener listener) {
lifecycleListeners.add(listener);
if (isDestroyed) {
listener.onDestroy();
} else if (isStarted) {
listener.onStart();
} else {
listener.onStop();
}
}
void onStart() {
isStarted = true;
for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {
lifecycleListener.onStart();
}
}
void onStop() {
isStarted = false;
for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {
lifecycleListener.onStop();
}
}
void onDestroy() {
isDestroyed = true;
for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {
lifecycleListener.onDestroy();
}
}
}
被观察者Activity、Fragment
public abstract class AbsActivity extends AppCompatActivity {
private final ActivityFragmentLifecycle lifecycle = new ActivityFragmentLifecycle();
@Override
protected void onStart() {
super.onStart();
lifecycle.onStart();
}
@Override
protected void onStop() {
super.onStop();
lifecycle.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
lifecycle.onDestroy();
}
public ActivityFragmentLifecycle getLifecycle() {
return lifecycle;
}
}
EventBus观察者,实现LifecycleListener
public class EventBusHandler implements LifecycleListener {
private boolean mRegistered = false;
//注册
public EventBusHandler register() {
if (!mRegistered) {
mRegistered = true;
EventCenter.getInstance().register(this);
L.d("register");
}
return this;
}
//解除注册
public EventBusHandler unregister() {
if (mRegistered) {
mRegistered = false;
EventCenter.getInstance().unregister(this);
L.d("unregister");
}
return this;
}
public synchronized void tryToRegisterIfNot() {
register();
}
@Override
public void onStart() {
register();
}
@Override
public void onStop() {
}
@Override
public void onDestroy() {
unregister();
}
}
EevetCenter
public class EventCenter {
private static final EventBus instance = new EventBus();
private EventCenter() {
}
public static EventBusHandler bindContainerAndHandler(Lifecycle container, EventBusHandler handler) {
container.addListener(handler);
return handler;
}
public static final EventBus getInstance() {
return instance;
}
}
activity代码调用
EventCenter.bindContainerAndHandler(getLifecycle(), new EventBusHandler() {
public void onEventMainThread(BlotterEvent event) {
//获取事件进行相关操作
}
}).tryToRegisterIfNot();
当activity生命终止时会通知调用EventBusHandler的onDestroy()解除注册。
当Activity,Fragment的生命周期可观察时,所做的远远不止于此。如Glide提供的DefaultConnectivityMonitor监听网络是否可用。