Skip to content

Commit 00753bb

Browse files
authored
Set view tree owners in parent class (#615)
1 parent 1b2927b commit 00753bb

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

android/libraries/rib-android-core/src/main/kotlin/com/uber/rib/core/CoreAppCompatActivity.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import android.os.Bundle
2020
import androidx.annotation.CallSuper
2121
import androidx.annotation.IntRange
2222
import androidx.appcompat.app.AppCompatActivity
23+
import androidx.lifecycle.ViewTreeLifecycleOwner
24+
import androidx.lifecycle.ViewTreeViewModelStoreOwner
25+
import androidx.savedstate.ViewTreeSavedStateRegistryOwner
2326

2427
/** Core Support v7 AppCompat Activity. */
2528
abstract class CoreAppCompatActivity : AppCompatActivity() {
@@ -32,6 +35,7 @@ abstract class CoreAppCompatActivity : AppCompatActivity() {
3235
activityDelegate = (applicationContext as HasActivityDelegate).activityDelegate()
3336
}
3437
super.onCreate(savedInstanceState)
38+
initViewTreeOwners()
3539
activityDelegate?.onCreate(savedInstanceState)
3640
}
3741

@@ -81,4 +85,16 @@ abstract class CoreAppCompatActivity : AppCompatActivity() {
8185
super.onActivityResult(requestCode, resultCode, data)
8286
activityDelegate?.onActivityResult(this, requestCode, resultCode, data)
8387
}
88+
89+
/**
90+
* [RibActivity] must call this since it does not use [ComponentActivity.setContentView] which
91+
* already handles this.
92+
*/
93+
private fun initViewTreeOwners() {
94+
// Set the view tree owners before setting the content view so that the inflation process
95+
// and attach listeners will see them already present
96+
ViewTreeLifecycleOwner.set(window.decorView, this)
97+
ViewTreeViewModelStoreOwner.set(window.decorView, this)
98+
ViewTreeSavedStateRegistryOwner.set(window.decorView, this)
99+
}
84100
}

android/libraries/rib-android/src/main/kotlin/com/uber/rib/core/RibActivity.kt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ import android.content.res.Configuration
2222
import android.os.Build
2323
import android.view.ViewGroup
2424
import androidx.annotation.CallSuper
25-
import androidx.lifecycle.ViewTreeLifecycleOwner
26-
import androidx.lifecycle.ViewTreeViewModelStoreOwner
27-
import androidx.savedstate.ViewTreeSavedStateRegistryOwner
2825
import com.uber.autodispose.lifecycle.CorrespondingEventsFunction
2926
import com.uber.autodispose.lifecycle.LifecycleEndedException
3027
import com.uber.autodispose.lifecycle.LifecycleNotStartedException
@@ -93,7 +90,6 @@ abstract class RibActivity :
9390
@CallSuper
9491
override fun onCreate(savedInstanceState: android.os.Bundle?) {
9592
super.onCreate(savedInstanceState)
96-
initViewTreeOwners()
9793
val rootViewGroup = findViewById<ViewGroup>(android.R.id.content)
9894
_lifecycleFlow.tryEmit(createOnCreateEvent(savedInstanceState))
9995
val wrappedBundle: Bundle? =
@@ -236,18 +232,6 @@ abstract class RibActivity :
236232
*/
237233
protected abstract fun createRouter(parentViewGroup: ViewGroup): ViewRouter<*, *>
238234

239-
/**
240-
* [RibActivity] must call this since it does not use [ComponentActivity.setContentView] which
241-
* already handles this.
242-
*/
243-
private fun initViewTreeOwners() {
244-
// Set the view tree owners before setting the content view so that the inflation process
245-
// and attach listeners will see them already present
246-
ViewTreeLifecycleOwner.set(window.decorView, this)
247-
ViewTreeViewModelStoreOwner.set(window.decorView, this)
248-
ViewTreeSavedStateRegistryOwner.set(window.decorView, this)
249-
}
250-
251235
companion object {
252236
/**
253237
* Figures out which corresponding next lifecycle event in which to unsubscribe, for Activities.

0 commit comments

Comments
 (0)