# Trello RX Lifecycle Dependency ###### tags: `Android Dependency` ![](https://i.imgur.com/gOK39s0.png) [TOC] # PreRequisites Need to use RxJava dependency. # Why we need it? As the name describes, it deals with the lifecycle. When using RxJava, if we try to exit the activity without unsubscribing in time, there would be still references to the activity causing memory leak. RxLifecycle was created to solve the memory leak caused by RxJava. # What can it do? It allows Observable's published events to be bound to current components to achieve lifecycle synchronization. When the current componen lifecycle ends, then subscription to the observable automatically cancels by its own. Conclusion: Automatically disconnects subscriptions to prevent memory leaks by monitoring the lifecycle of Activity and Fragment. # How to use it? ## Add dependency **build.gradle(:app)** ```java= //Trello RxLifecycle def rxLifecycle = '3.0.0' implementation "com.trello.rxlifecycle3:rxlifecycle:${rxLifecycle}" implementation "com.trello.rxlifecycle3:rxlifecycle-components:${rxLifecycle}" ``` ## Bound Activity/Fragment to right base Activity/Fragment should be bounded to **RxAppCompatActivity/RxFragment**(They will be available by applying implementation of trello.lifecycle in build.gradle). When bounded to **RxAppCompatActivity/RxFragment**, these will be available for use: 1. RxAppCompatActivity 2. RxAppCompatDialogFragment 3. ExDialogFragment 4. RxFragment 5. RxFragmentActivity ## Bind to container lifecycle >In an activity, we can use: >1. bindUtilEvent(@NonNull ActivityEvent event) >1. bindToLifecycle()[color=pink] >In an fragment, we can use: >1. bindUtilEvent(@NonNull FragmentEvent event) >1. bindToLifecycle()[color=lightblue] ### **bindUtilEvent** This one states in which stage of lifecycle to unsubscribe. Below, we can see the different Events we can use for this method. **Fragment Events:** ```java= public enum FragmentEvent { ATTACH, CREATE, CREATE_VIEW, START, RESUME, PAUSE, STOP, DESTROY_VIEW, DESTROY, DETACH } ``` **Activity Events:** ```java= public enum ActivityEvent { CREATE, START, RESUME, PAUSE, STOP, DESTROY } ``` **Example:** In this case, it will just cancel observable subscription in onDestroy. ```java= override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) Observable.interval(1, TimeUnit.SECONDS) .doOnDispose { Log.i(TAG, "Unsubscribing subscription from onDestory()") } .compose(bindUntilEvent(ActivityEvent.DESTROY)) .subscribe { Log.i(TAG, "Started in onCreate(), running until in onDestroy(): $it") } } ``` ### **bindToLifecycle** By using bindToLifecycle, the subscription will be cariied out in specific lifecycle. **Example:** In this API call where we bind it to lifecycle, the subscription will be dropped when **onPause** is encountered. ```java= apiHelper.createApiService(ApiService.class) .getMatchType() .compose(bindLifeCycle(true)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new BaseObserver<BaseResponse<MatchType>>() { } ``` # Dependency Base Files **RxFragment & RxAppCompatActivity** are from rxlifecycle-components support. ```java= public abstract class RxAppCompatActivity extends AppCompatActivity implements LifecycleProvider<ActivityEvent> { private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create(); @Override @NonNull @CheckResult public final Observable<ActivityEvent> lifecycle() { return lifecycleSubject.hide(); } @Override @NonNull @CheckResult public final <T> LifecycleTransformer<T> bindUntilEvent(@NonNull ActivityEvent event) { return RxLifecycle.bindUntilEvent(lifecycleSubject, event); } @Override @NonNull @CheckResult public final <T> LifecycleTransformer<T> bindToLifecycle() { return RxLifecycleAndroid.bindActivity(lifecycleSubject); } @Override @CallSuper protected void onCreate(@Nullable 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(); } } ``` # Reference [Can refer to more information here](https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/744000/)