Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose : ViewTreeLifecycleOwner not found

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"
like image 225
Arrowsome Avatar asked Dec 31 '22 14:12

Arrowsome


2 Answers

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

Add:

implementation 'androidx.appcompat:appcompat:1.3.0'
like image 183
Gabriele Mariotti Avatar answered Jan 07 '23 18:01

Gabriele Mariotti


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

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

like image 33
tricknology Avatar answered Jan 07 '23 20:01

tricknology