I'm getting IllegalStateException
error while using Compose in my fragment.
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.constraintlayout.widget.ConstraintLayout
The activity is AppCompatActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
I'm using Navigation Component
to navigate between fragments. and this is Activities XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/appContent"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/main_navigation_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
And Fragment:
class WelcomeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(
DisposeOnLifecycleDestroyed(viewLifecycleOwner)
)
setContent {
MaterialTheme {
Text(text = "Welcome to App")
}
}
}
}
}
Dependencies and versions:
FragmentVersion = '1.3.3'
CONSTRAINT_LAYOUT_VERSION = '2.1.0'
KOTLIN_VERSION = '1.5.21'
ACTIVITY_KTX_VERSION = '1.3.1'
COMPOSE_VERSION = '1.0.0'
COMPOSE_ACTIVITY = '1.3.1'
COMPOSE_VIEWMODEL = '1.0.0-alpha07'
COMPOSE_CONSTRAINT_VERSION = '1.0.0-beta02'
APP_COMPAT_VERSION = '1.3.1'