Skip to content

Commit

Permalink
Set view tree owners in parent class
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarr21 committed Sep 12, 2023
1 parent 1b2927b commit ee76bc3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import android.os.Bundle
import androidx.annotation.CallSuper
import androidx.annotation.IntRange
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewTreeLifecycleOwner
import androidx.lifecycle.ViewTreeViewModelStoreOwner
import androidx.savedstate.ViewTreeSavedStateRegistryOwner

/** Core Support v7 AppCompat Activity. */
abstract class CoreAppCompatActivity : AppCompatActivity() {
Expand All @@ -32,6 +35,7 @@ abstract class CoreAppCompatActivity : AppCompatActivity() {
activityDelegate = (applicationContext as HasActivityDelegate).activityDelegate()
}
super.onCreate(savedInstanceState)
initViewTreeOwners()
activityDelegate?.onCreate(savedInstanceState)
}

Expand Down Expand Up @@ -81,4 +85,16 @@ abstract class CoreAppCompatActivity : AppCompatActivity() {
super.onActivityResult(requestCode, resultCode, data)
activityDelegate?.onActivityResult(this, requestCode, resultCode, data)
}

/**
* [RibActivity] must call this since it does not use [ComponentActivity.setContentView] which
* already handles this.
*/
private fun initViewTreeOwners() {
// Set the view tree owners before setting the content view so that the inflation process
// and attach listeners will see them already present
ViewTreeLifecycleOwner.set(window.decorView, this)
ViewTreeViewModelStoreOwner.set(window.decorView, this)
ViewTreeSavedStateRegistryOwner.set(window.decorView, this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import android.content.res.Configuration
import android.os.Build
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.lifecycle.ViewTreeLifecycleOwner
import androidx.lifecycle.ViewTreeViewModelStoreOwner
import androidx.savedstate.ViewTreeSavedStateRegistryOwner
import com.uber.autodispose.lifecycle.CorrespondingEventsFunction
import com.uber.autodispose.lifecycle.LifecycleEndedException
import com.uber.autodispose.lifecycle.LifecycleNotStartedException
Expand Down Expand Up @@ -93,7 +90,6 @@ abstract class RibActivity :
@CallSuper
override fun onCreate(savedInstanceState: android.os.Bundle?) {
super.onCreate(savedInstanceState)
initViewTreeOwners()
val rootViewGroup = findViewById<ViewGroup>(android.R.id.content)
_lifecycleFlow.tryEmit(createOnCreateEvent(savedInstanceState))
val wrappedBundle: Bundle? =
Expand Down Expand Up @@ -236,18 +232,6 @@ abstract class RibActivity :
*/
protected abstract fun createRouter(parentViewGroup: ViewGroup): ViewRouter<*, *>

/**
* [RibActivity] must call this since it does not use [ComponentActivity.setContentView] which
* already handles this.
*/
private fun initViewTreeOwners() {
// Set the view tree owners before setting the content view so that the inflation process
// and attach listeners will see them already present
ViewTreeLifecycleOwner.set(window.decorView, this)
ViewTreeViewModelStoreOwner.set(window.decorView, this)
ViewTreeSavedStateRegistryOwner.set(window.decorView, this)
}

companion object {
/**
* Figures out which corresponding next lifecycle event in which to unsubscribe, for Activities.
Expand Down

0 comments on commit ee76bc3

Please sign in to comment.