Skip to content

Commit 618ac4e

Browse files
authored
Merge pull request #22 from RedMadRobot/feature/viewbinding-ktx-4.1.2-1
viewbinding-ktx: 4.1.2-1
2 parents fc1ce17 + 1d10240 commit 618ac4e

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

Diff for: viewbinding-ktx/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## Unreleased
22

3+
## 4.1.2-1 (2021-03-07)
4+
5+
### Fixed
6+
7+
- Add workaround for the case when fragment view changed without triggering ON_DESTROY
8+
- Save binding only if fragment view is not destroyed
9+
310
## 4.1.2-0 (2021-03-01)
411

512
### Dependencies

Diff for: viewbinding-ktx/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
id("redmadrobot.publish")
66
}
77

8-
version = "4.1.2-0"
8+
version = "4.1.2-1"
99
description = "A set of Kotlin extensions for dealing with ViewBinding"
1010

1111
android {

Diff for: viewbinding-ktx/src/main/kotlin/ViewBindingDelegate.kt

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.redmadrobot.extensions.viewbinding
22

3-
import android.os.Handler
4-
import android.os.Looper
53
import androidx.fragment.app.Fragment
64
import androidx.lifecycle.Lifecycle
5+
import androidx.lifecycle.Lifecycle.Event.ON_DESTROY
6+
import androidx.lifecycle.Lifecycle.State.INITIALIZED
77
import androidx.lifecycle.LifecycleEventObserver
88
import androidx.lifecycle.LifecycleOwner
99
import androidx.viewbinding.ViewBinding
@@ -43,28 +43,43 @@ internal class ViewBindingDelegate<VB : ViewBinding> constructor(
4343
) : ReadOnlyProperty<Any?, VB>, LifecycleEventObserver {
4444

4545
private var binding: VB? = null
46-
private val handler = Handler(Looper.getMainLooper())
46+
47+
private val fragmentViewNotDestroyed: Boolean
48+
get() = fragment.viewLifecycleOwner.lifecycle.currentState.isAtLeast(INITIALIZED)
4749

4850
init {
4951
fragment.viewLifecycleOwnerLiveData.observe(fragment) {
5052
it.lifecycle.addObserver(this)
5153
}
5254
}
5355

54-
override fun getValue(thisRef: Any?, property: KProperty<*>): VB = binding ?: obtainBinding()
56+
override fun getValue(thisRef: Any?, property: KProperty<*>): VB {
57+
checkFragmentViewNotChanged()
58+
return binding ?: obtainBinding()
59+
}
60+
61+
/**
62+
* In some cases [Fragment] can change view without triggering [ON_DESTROY] on the old view.
63+
* So we should check that bound view is equal to fragment view.
64+
*/
65+
private fun checkFragmentViewNotChanged() {
66+
val boundView = binding?.root ?: return
67+
if (boundView != fragment.view) binding = null
68+
}
5569

5670
private fun obtainBinding(): VB {
5771
val view = checkNotNull(fragment.view) {
5872
"ViewBinding is only valid between onCreateView and onDestroyView."
5973
}
60-
return viewBindingClass.bind(view)
61-
.also { binding = it }
74+
return viewBindingClass.bind(view).also {
75+
binding = it.takeIf { fragmentViewNotDestroyed }
76+
}
6277
}
6378

6479
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
65-
if (event == Lifecycle.Event.ON_DESTROY) {
80+
if (event == ON_DESTROY) {
6681
source.lifecycle.removeObserver(this)
67-
handler.post { binding = null }
82+
binding = null
6883
}
6984
}
7085
}

0 commit comments

Comments
 (0)