In Kotlin, while opening an activity or a fragment,I can call an api in this format and get response without any issue:
compositeDisposable.add(dataManager
.getHistory()
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
.subscribe({ response ->
isSwipeRefreshCalled.set(false)
setIsLoading(false)
if (response.isSuccessful) {
setHasErrorMessage(false)
navigator.getHistory()
} else {
setHasErrorMessage(true)
navigator.showErrorMessage(response.responseMessage)
}
}, { throwable ->
setIsLoading(false)
isSwipeRefreshCalled.set(false)
navigator.handleError(throwable)
})
)
,but whenever I clicked backButton and go back to that activity/fragment,although compositeDisposable has been called, nothing has happened and neither response block got called nor throw an exception.
be noted that compositeDisposable.dispose()
is called when activity/fragment destroyed (onDestroy()/onDestroyView())
Be glad someone assist me.
this is too big for a comment. I'm not sure this is the issue, but I did notice something in the description of the problem.
From the comments and the question I gathered that you are doing something like:
onViewCreated:
// add to composite disposable
onViewDestroyed:
// dispose the composite disposable
The problem is that when you call compositeDisposable.dispose()
you actually render this composite disposable unusable. In other words, every time you try to add a disposable to it, it won't work because the composite disposable already disposed. This can be an issue if the composite disposable stays in memory, i.e., if you have it as a field of a viewModel
that doesn't get recreated. The next time you come and use the same composite disposable it won't work.
Instead, you can use compositeDisposable.clear()
, which will remove all disposables from the container and dispose of them. However, the container is not disposed and can be used for more disposables.