Skip to content

Commit

Permalink
FIX: replaceFragment lifecycle bug (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoJiang authored Feb 13, 2023
1 parent a54cc52 commit e692238
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ kotlin.code.style=official
android.nonTransitiveRClass=true

KOTLIN_PLUGIN_ID=com.kanyun.kace
VERSION_NAME=1.8.0-1.0.3-SNAPSHOT
VERSION_NAME=1.8.0-1.0.4-SNAPSHOT

GROUP=com.kanyun.kace

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,29 @@ sealed interface AndroidExtensionsComponent {

fun AndroidExtensionsComponent(
owner: AndroidExtensionsBase,
onDestroy: () -> Unit
onViewDestroy: () -> Unit,
onComponentDestroy: () -> Unit
): AndroidExtensionsComponent {
return when (owner) {
is Activity -> AndroidExtensionsActivity(owner, onDestroy)
is Fragment -> AndroidExtensionsFragment(owner, onDestroy)
is Activity -> AndroidExtensionsActivity(owner, onViewDestroy, onComponentDestroy)
is Fragment -> AndroidExtensionsFragment(owner, onViewDestroy, onComponentDestroy)
else -> throw UnsupportedOperationException()
}
}

class AndroidExtensionsActivity(
private val activity: Activity,
onDestroy: () -> Unit
onViewDestroy: () -> Unit,
onComponentDestroy: () -> Unit
) : AndroidExtensionsComponent {

init {
if (activity is LifecycleOwner) {
activity.lifecycle.addObserver(object : KaceLifecycleObserver() {
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
onDestroy()
onViewDestroy()
onComponentDestroy()
}
})
}
Expand All @@ -59,18 +62,25 @@ class AndroidExtensionsActivity(

class AndroidExtensionsFragment(
private val fragment: Fragment,
onDestroy: () -> Unit
onViewDestroy: () -> Unit,
onComponentDestroy: () -> Unit
) : AndroidExtensionsComponent {

init {
fragment.viewLifecycleOwnerLiveData.observe(fragment) {
it?.lifecycle?.addObserver(object : KaceLifecycleObserver() {
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
onDestroy()
onViewDestroy()
}
})
}
fragment.lifecycle.addObserver(object : KaceLifecycleObserver() {
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
onComponentDestroy()
}
})
}

override fun <V : View?> findViewById(id: Int): V? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AndroidExtensionsImpl : AndroidExtensions {

private fun initComponent(owner: AndroidExtensionsBase) {
if (component == null) {
component = AndroidExtensionsComponent(owner) { destroy() }
component = AndroidExtensionsComponent(owner, this::onViewDestroy, this::onComponentDestroy)
}
}

Expand All @@ -42,7 +42,11 @@ class AndroidExtensionsImpl : AndroidExtensions {
return cached?.getOrPut(id) { component!!.findViewById(id) } as T
}

private fun destroy() {
private fun onViewDestroy() {
cached?.clear()
}

private fun onComponentDestroy() {
component = null
}
}

0 comments on commit e692238

Please sign in to comment.