androidrx-javarx-java2rx-kotlin2

Do we need to call onComplete() of subject in onDestroy() method of activity?


If in ActivityA, it keep reference to

private val subjectA: PublishSubject<ObjectB> = PublishSubject.create()
private var dispose: Disposable? = null

Does we need to call onComplete() method of subject in onDestroy() method of Activity

override fun onDestroy() {
    subjectA.onComplete()
    dispose?.dispose()
    super.onDestroy()
}

Solution

  • There is no need(and you should not call) of calling onComplete in onDestroy of Activity/fragment as it is used to issue the stream that the data transfer is completed and when view is destroyed it is not same as completion.You should just dispose the Disposable as Dispose is a different use-case and stop any further push-based notifications instead of telling the observer onError/onComplete.

    From documentation:

    Notifies the Observer that the Observable has finished sending push-based notifications.

    To understand difference between dispose and complete refer below link's: