trello-archive / RxLifecycle

Lifecycle handling APIs for Android apps using RxJava
Apache License 2.0
7.71k stars 638 forks source link

subscribe method still call after onDestroy method #322

Closed lkkz closed 4 years ago

lkkz commented 4 years ago

I met a problem, I use observeOn (AndroidSchedulers. MainThread (), cut to the main thread, has in onDestroy call the subscribe, have a chance discovery, below is the code and log

code: class KotlinActivity : RxAppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    Log.d(TAG, "onCreate()")

    setContentView(R.layout.activity_main)

    Observable.interval(0, 1,TimeUnit.SECONDS)
            .doOnDispose { Log.i(TAG, "Unsubscribing subscription from onCreate()") }
            .bindUntilEvent(this, ActivityEvent.DESTROY)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { num -> Log.i(TAG, "Started in onCreate(), running until onDestroy(): " + num) }
}
override fun onDestroy() {
    super.onDestroy()
    Log.d(TAG, "onDestroy()")
}

companion object {
    private val TAG = "RxLifecycle-Kotlin"
}

}

log:

2020-06-08 17:01:11.510 8485-8485/com.trello.rxlifecycle4.sample I/RxLifecycle-Kotlin: Started in onCreate(), running until onDestroy(): 0 2020-06-08 17:01:12.510 8485-8485/com.trello.rxlifecycle4.sample I/RxLifecycle-Kotlin: Unsubscribing subscription from onCreate() 2020-06-08 17:01:12.511 8485-8485/com.trello.rxlifecycle4.sample D/RxLifecycle-Kotlin: onDestroy() 2020-06-08 17:01:12.518 8485-8485/com.trello.rxlifecycle4.sample I/RxLifecycle-Kotlin: Started in onCreate(), running until onDestroy(): 1

dlew commented 4 years ago

Call bindUntilEvent() after observeOn() and it should work correctly. For more info, see #224.

lkkz commented 4 years ago

thanks!

jiongjiongxia commented 3 years ago

@dlew Hi, I used rxlife1.0 and rxjava2 in my project,It's seems OK to write like 'call bindUntilEvent() before observeOn()' in before,but when I used rxlife4.x and rxjava3 , I had the same problem,but this happens only occasionally,Is this due to the change caused by 4.x or rxjava3,Or means the old version will also happen, but I didn't find it?