6

I'm getting this error while using Compose in my fragment which works fine in case of XML

ViewTreeLifecycleOwner not found from androidx.fragment.app.FragmentContainerView

I'm using a single activity approach without using Jetpack Navigation component

Activity:

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_nav)
  supportFragmentManager.commit {
    setReorderingAllowed(true)
    add<InboxFragment>(R.id.nav_fragmentContainerView_appNav)
  }
}
<androidx.fragment.app.FragmentContainerView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/nav_fragmentContainerView_appNav"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

Fragment:

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
): View {
  return ComposeView(requireContext()).apply {
    setContent {
      Text(text = "HELLO FRIEND!")
    }
  }
}

Dependencies:

def fragment_version = "1.3.3"
implementation("androidx.fragment:fragment-ktx:$fragment_version")

def compose_version = "1.0.0-beta06"
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.activity:activity-compose:1.3.0-alpha07"
classpath "com.android.tools.build:gradle:7.0.0-alpha15"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"
Arrowsome
  • 2,649
  • 3
  • 10
  • 35
  • Are you using an AppCompatActivity? – Gabriele Mariotti May 18 '21 at 12:49
  • @GabrieleMariotti yeah `androidx.appcompat.app.AppCompatActivity` – Arrowsome May 18 '21 at 12:59
  • The support for `ViewTreeLifecycleOwner` was introduced in the AppCompat with the version `1.3.0`. Use: `implementation 'androidx.appcompat:appcompat:1.3.0-rc01'` – Gabriele Mariotti May 18 '21 at 13:00
  • Does this answer your question? [ViewTreeLifecycleOwner not found from DecorView@2da7146\[MyActivity\]](https://stackoverflow.com/questions/66382502/viewtreelifecycleowner-not-found-from-decorview2da7146myactivity) – Mahozad Sep 18 '21 at 07:41
  • I suggest that you remove "Jetpack Comppose" from the title and tags, as this issue is unrelated to Compose. – Agent_L Aug 15 '23 at 11:00

4 Answers4

12

Since you are using an AppCompatActivity, only the appcompat 1.3 versions populate the ViewTreeLifecycleOwner.

Add:

implementation 'androidx.appcompat:appcompat:1.3.0'
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
3

None of these worked and there seems to be an open issue with BottomSheetDialog.

https://issuetracker.google.com/issues/261078350

tricknology
  • 1,092
  • 1
  • 15
  • 27
2

It's just a simple name change, from class with static method to extension methods:

ViewTreeLifecycleOwner.get(view) changed to View.findViewTreeLifecycleOwner()
ViewTreeLifecycleOwner.set() to View.setViewTreeLifecycleOwner(lifecycleOwner: LifecycleOwner?)

Agent_L
  • 4,960
  • 28
  • 30
0

In my case, I encountered this error when using coil with 2.3.0 Changing the coil version to 2.2.2 solved the problem.

ayhanunal
  • 83
  • 1
  • 7
  • 2
    They just changed the way you set it. ViewTreeLifecycleOwner.set(view, owner) -> view.setViewTreeLifecycleOwner(owner) – badoualy May 03 '23 at 06:48