package bigfat.com.rxbusdemo; import android.os.Bundle; import android.support.annotation.CallSuper; import android.support.annotation.CheckResult; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import bigfat.com.rxbusdemo.rxbus.RxBus; import com.trello.rxlifecycle.ActivityEvent; import com.trello.rxlifecycle.ActivityLifecycleProvider; import com.trello.rxlifecycle.RxLifecycle; import rx.Observable; import rx.subjects.BehaviorSubject; /** * Created by yueban on 15:06 31/3/16. * Email: fbzhh007@gmail.com * QQ: 343278606 */ public class BaseActivity extends AppCompatActivity implements ActivityLifecycleProvider { private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create(); protected final Observable<Object> mBus = RxBus .toObservable() .compose(bindToLifecycle()); protected final Observable<Object> mBusSticky = RxBus .toObservableSticky() .compose(bindToLifecycle()); @Override @NonNull @CheckResult public final Observable<ActivityEvent> lifecycle() { return lifecycleSubject.asObservable(); } @Override @NonNull @CheckResult public final <T> Observable.Transformer<T, T> bindUntilEvent(@NonNull ActivityEvent event) { return RxLifecycle.bindUntilEvent(lifecycleSubject, event); } @Override @NonNull @CheckResult public final <T> Observable.Transformer<T, T> bindToLifecycle() { return RxLifecycle.bindActivity(lifecycleSubject); } @Override @CallSuper protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); lifecycleSubject.onNext(ActivityEvent.CREATE); } @Override @CallSuper protected void onStart() { super.onStart(); lifecycleSubject.onNext(ActivityEvent.START); } @Override @CallSuper protected void onResume() { super.onResume(); lifecycleSubject.onNext(ActivityEvent.RESUME); } @Override @CallSuper protected void onPause() { lifecycleSubject.onNext(ActivityEvent.PAUSE); super.onPause(); } @Override @CallSuper protected void onStop() { lifecycleSubject.onNext(ActivityEvent.STOP); super.onStop(); } @Override @CallSuper protected void onDestroy() { lifecycleSubject.onNext(ActivityEvent.DESTROY); super.onDestroy(); } }