Search code examples
androidrx-javarx-kotlin

RxKotlin - Process gets terminated when dispose is called


i am trying the upload the image through rxkotlin, my problem is when i close my screen in my application, the uploading process gets terminated because am adding that subscription to my disposal, so when my screen closes or destroys i will be disposing. because of this dispose my job getting terminated, since anyway i need to dispose else it my leads to the memory leak

fun uploadImage(imagePath : String){
   uploadToServer(imagePath)
        .subscribe()
        .addTo(disposal)   
}

override onCleared(){
   disposal.dispose()
}

So in this way i doing this, is there is any other way to continue this work, even after the screen destroys or cleared without memory leak ?


Solution

  • ViewModel are independent of activity/fragment lifecycles or just their configuration changes class, because of that onCleared is called only after the activity is finished. So don't dispose the Rx variables on onCleared. It is better to dispose them on onDestroy

    override onDestory(){
    super.onDestroy()
    disposal.dispose()
    }