public TypeSpec generate() { return TypeSpec.classBuilder(getClassName()) .addJavadoc("Generated by Kickback. (https://github.com/skydoves/Kickback).\n") .addModifiers(Modifier.PUBLIC) .addSuperinterface(LifecycleObserver.class) .addFields(getKickbackFields()) .addMethods(getSetterMethodSpecs()) .addMethods(getGetterMethodSpecs()) .addMethods(getFreeMethodSpecs()) .addMethod(getBoxNameSpec()) .addMethods(getElementNameListSpecs()) .addMethod(getFreeAllSpec()) .addMethod(setLifecycleObserverSpec()) .addMethod(getLifecycleObserverSpec()) .build(); }
/** * Like {@link #trackEvent(Event, Context, Bundle)}, but also starts a performance Trace. * * @param event {@link #trackEvent(Event, Context, Bundle)} * @param context {@link #trackEvent(Event, Context, Bundle)} * @param bundle {@link #trackEvent(Event, Context, Bundle)} * @param container lifecycle to attach Trace to (onDestroy will stop the trace). */ public static void trackEventStart(@NonNull final Event event, @NonNull final Context context, @Nullable final Bundle bundle, @Nullable final LifecycleOwner container) { trackEvent(event, context, bundle); if (sRunningEvents.containsKey(event)) { LOGGER.error("Event {} triggered while performance counting for it was already in " + "progress! Finishing now", event, new Throwable()); trackEventFinish(event); } Trace t = FirebasePerformance.startTrace(event.track()); sRunningEvents.put(event, t); t.start(); if (container != null) { // Container can be null if we test cross-activity lifecycle container.getLifecycle().addObserver(new LifecycleObserver() { @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) public void stopTrace() { trackEventFinish(event); } }); } }
@Override public void onStart() { //将 LifecycleObserver 注册给 LifecycleOwner 后 @OnLifecycleEvent 才可以正常使用 if (mRootView != null && mRootView instanceof LifecycleOwner) { ((LifecycleOwner) mRootView).getLifecycle().addObserver(this); if (mModel!= null && mModel instanceof LifecycleObserver){ ((LifecycleOwner) mRootView).getLifecycle().addObserver((LifecycleObserver) mModel); } } if (useEventBus())//如果要使用 Eventbus 请将此方法返回 true EventBus.getDefault().register(this);//注册 Eventbus }
@Override public TypeSpec defineClass() { return TypeSpec.classBuilder(producer.getClassName(annotatedElement)) .addSuperinterface(LifecycleObserver.class) .addField(LifecycleAwareObserver.class, FIELD_OBSERVER, Modifier.PRIVATE) .addModifiers(Modifier.PUBLIC, Modifier.FINAL) .build(); }
/** * Creates a new scope for the given lifecycle owner (e.g. a Fragment or an Activity). * Note: The lifetime is * attached to the {@link android.arch.lifecycle.Lifecycle} AND the parent scope. So the returned * scope is destroyed if either of them is destroyed. */ public static Scope createScope(@Nullable Scope parent, LifecycleOwner lifecycleOwner) { Scope scope = new Scope(lifecycleOwner.toString(), parent); lifecycleOwner.getLifecycle().addObserver(new LifecycleObserver() { @OnLifecycleEvent(Event.ON_DESTROY) void onDestroy() { scope.destroy(); } }); if (lifecycleOwner instanceof Context) { scope.put(Android.NAME_CONTEXT, lifecycleOwner); } return scope; }
private MethodSpec getLifecycleObserverSpec() { return MethodSpec.methodBuilder("getLifecycleObserver") .addModifiers(Modifier.PUBLIC) .returns(LifecycleObserver.class) .addStatement("return this") .build(); }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Set the DataBinding mBinding = DataBindingUtil.setContentView(this, initView(savedInstanceState)); initData(savedInstanceState); if (mViewModel != null) { getLifecycle().addObserver((LifecycleObserver) mViewModel); } }
@Override protected void onDestroy() { super.onDestroy(); this.mBinding = null; this.mViewModelFactory = null; //Removed LifecycleObserver if (mViewModel != null) { getLifecycle().removeObserver((LifecycleObserver) mViewModel); } this.mViewModel = null; }
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mRootView = initView(inflater, container, savedInstanceState); if (mViewModel != null) { getLifecycle().addObserver((LifecycleObserver) mViewModel); } //Visible, and is the first time to load if (mVisible && mFirst) { onFragmentVisibleChange(true); } return mRootView; }
@Override public void onDestroy() { super.onDestroy(); this.mBinding = null; this.mRootView = null; this.mViewModelFactory = null; //Removed LifecycleObserver if (mViewModel != null) { getLifecycle().removeObserver((LifecycleObserver) mViewModel); } this.mViewModel = null; }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置DataBinding mBinding = DataBindingUtil.setContentView(this, initView(savedInstanceState)); initData(savedInstanceState); if (mViewModel != null) { getLifecycle().addObserver((LifecycleObserver) mViewModel); } }
@Override protected void onDestroy() { super.onDestroy(); this.mBinding = null; this.mViewModelFactory = null; //移除LifecycleObserver if (mViewModel != null) { getLifecycle().removeObserver((LifecycleObserver) mViewModel); } this.mViewModel = null; }
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mRootView = initView(inflater, container, savedInstanceState); if (mViewModel != null) { getLifecycle().addObserver((LifecycleObserver) mViewModel); } //可见,并且是首次加载 if (mVisible && mFirst) { onFragmentVisibleChange(true); } return mRootView; }
@Override public void onDestroy() { super.onDestroy(); this.mBinding = null; this.mRootView = null; this.mViewModelFactory = null; //移除LifecycleObserver if (mViewModel != null) { getLifecycle().removeObserver((LifecycleObserver) mViewModel); } this.mViewModel = null; }
@Override protected void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); getLifecycle().addObserver(new LifecycleObserver() { @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) public void cleanDisposables() { getLifecycle().removeObserver(this); mDisposeOnDestroy.dispose(); } }); }
@Override public void addObserver(LifecycleObserver observer) { }
@Override public void removeObserver(LifecycleObserver observer) { }