以前的lifecycle-component采用的注解处理器, 编译时动态生成adapter进行宿主生命周期的监听, 现在的版本是直接在代码里面写死.
demo
public class LifecycleAct extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("AndroidTest", "LifecycleAct:onCreate");
getLifecycle().addObserver(new Presenter());
}
@Override protected void onResume() {...}
@Override protected void onStart() {...}
@Override protected void onPause() {...}
@Override protected void onStop() {...}
@Override protected void onDestroy() {...}
}
public class Presenter implements LifecycleObserver {
private final String TAG = "AndroidTest";
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreate() {}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {}
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny() {}
}
当Activity生命周期发生变化时如何通知ILifecycleObserver对应的方法?
一、添加监听
1.1 LifecycleRegistry.addObserver
@Override
public void addObserver(@NonNull LifecycleObserver observer) {
State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;
// 每个Presenter都会创建一个对应的ObserverWithState;
ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);
...
}
1.2 ObserverWithState
static class ObserverWithState {
State mState;
GenericLifecycleObserver mLifecycleObserver;
ObserverWithState(LifecycleObserver observer, State initialState) {
// 这里使用了一个适配器模式, 适配器的话, 其实完全没必要结合ListView-Adapter去看,
// 可以结合这里, 也可以结合FutureTask去看. 返回值最终指向的是ReflectiveGenericLifecycleObserver
mLifecycleObserver = Lifecycling.getCallback(observer);
mState = initialState;
}
void dispatchEvent(LifecycleOwner owner, Event event) {
State newState = getStateAfter(event);
mState = min(mState, newState);
mLifecycleObserver.onStateChanged(owner, event);
mState = newState;
}
}
二、生命周期的回调
2.1 ReflectiveGenericLifecycleObserver.onStateChanged
@Override
public void onStateChanged(LifecycleOwner source, Event event) {
// 这里面以注解的方式, 获取被对应event注解的方法, 然后调用该方法,
// 从而实现生命周期的回调
mInfo.invokeCallbacks(source, event, mWrapped);
}
2.2 注解的方式调用对应的方法ClassesInfoCache.createInfo
这个方法其实是属于添加监听时的一个方法
private CallbackInfo createInfo(Class klass, @Nullable Method[] declaredMethods) {
Class superclass = klass.getSuperclass();
Map<MethodReference, Lifecycle.Event> handlerToEvent = new HashMap<>();
if (superclass != null) {
CallbackInfo superInfo = getInfo(superclass);
if (superInfo != null) {
handlerToEvent.putAll(superInfo.mHandlerToEvent);
}
}
Class[] interfaces = klass.getInterfaces();
for (Class intrfc : interfaces) {
for (Map.Entry<MethodReference, Lifecycle.Event> entry : getInfo(
intrfc).mHandlerToEvent.entrySet()) {
verifyAndPutHandler(handlerToEvent, entry.getKey(), entry.getValue(), klass);
}
}
Method[] methods = declaredMethods != null ? declaredMethods : getDeclaredMethods(klass);
boolean hasLifecycleMethods = false;
for (Method method : methods) {
OnLifecycleEvent annotation = method.getAnnotation(OnLifecycleEvent.class);
if (annotation == null) {
continue;
}
hasLifecycleMethods = true;
Class<?>[] params = method.getParameterTypes();
int callType = CALL_TYPE_NO_ARG;
if (params.length > 0) {
callType = CALL_TYPE_PROVIDER;
}
Lifecycle.Event event = annotation.value();
if (params.length > 1) {
callType = CALL_TYPE_PROVIDER_WITH_EVENT;
}
MethodReference methodReference = new MethodReference(callType, method);
verifyAndPutHandler(handlerToEvent, methodReference, event, klass);
}
CallbackInfo info = new CallbackInfo(handlerToEvent);
mCallbackMap.put(klass, info);
mHasLifecycleMethods.put(klass, hasLifecycleMethods);
return info;
}
上面代码进行了注解与方法名以及方法参数的绑定, 后续在调用dispatchEvent时通过Event获取对应的方法名.
看完这个过程, 感觉跟EventBus的方法是很类似的了, 类Presenter、注解Event、方法名、参数进行绑定, 触发时通过Event去找到对应的类以及方法然后进行调用.
网友评论