Skip to content

Commit f4df66e

Browse files
author
jbwoods
committed
Refactor Fragment-in-Compose snippet to use AndroidFragment
Replaced the `AndroidViewBinding` implementation with the simpler `AndroidFragment` composable from the `androidx.fragment:fragment-compose` library. This change also includes: - Adding the `fragment-compose` dependency. - Removing the now-unused XML layout file for the fragment container.
1 parent 977c8e0 commit f4df66e

File tree

4 files changed

+6
-30
lines changed

4 files changed

+6
-30
lines changed

compose/snippets/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ dependencies {
109109
implementation(libs.androidx.emoji2.views)
110110
implementation(libs.androidx.lifecycle.runtime.compose)
111111

112+
implementation(libs.androidx.fragment.compose)
112113
implementation(libs.androidx.fragment.ktx)
113114
implementation(libs.androidx.coordinator.layout)
114115
implementation(libs.google.android.material)

compose/snippets/src/main/java/com/example/compose/snippets/interop/InteroperabilityAPIsSnippets.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ import androidx.compose.ui.tooling.preview.Preview
5151
import androidx.compose.ui.viewinterop.AndroidView
5252
import androidx.compose.ui.viewinterop.AndroidViewBinding
5353
import androidx.fragment.app.Fragment
54+
import androidx.fragment.compose.AndroidFragment
5455
import com.example.compose.snippets.MyActivity
5556
import com.example.compose.snippets.R
5657
import com.example.compose.snippets.databinding.ExampleLayoutBinding
5758
import com.example.compose.snippets.databinding.FragmentExampleBinding
58-
import com.example.compose.snippets.databinding.MyFragmentLayoutBinding
5959

6060
// [START android_compose_interop_apis_compose_in_views]
6161
class ExampleActivity : ComponentActivity() {
@@ -272,10 +272,7 @@ fun AndroidViewBindingExample() {
272272
// [START android_compose_interop_apis_fragments_in_compose]
273273
@Composable
274274
fun FragmentInComposeExample() {
275-
AndroidViewBinding(MyFragmentLayoutBinding::inflate) {
276-
val myFragment = fragmentContainerView.getFragment<MyFragment>()
277-
// ...
278-
}
275+
AndroidFragment<MyFragment>()
279276
}
280277

281278
// [START_EXCLUDE silent]

compose/snippets/src/main/res/layout/my_fragment_layout.xml

Lines changed: 0 additions & 23 deletions
This file was deleted.

gradle/libs.versions.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ androidx-corektx = "1.17.0"
1515
androidx-credentials = "1.6.0-beta03"
1616
androidx-credentials-play-services-auth = "1.6.0-beta03"
1717
androidx-emoji2-views = "1.6.0"
18-
androidx-fragment-ktx = "1.8.9"
18+
androidx-fragment = "1.8.9"
1919
androidx-glance-appwidget = "1.1.1"
2020
androidx-lifecycle-compose = "2.9.4"
2121
androidx-lifecycle-runtime-compose = "2.9.4"
@@ -128,7 +128,8 @@ androidx-core-splashscreen = { module = "androidx.core:core-splashscreen", versi
128128
androidx-credentials = { module = "androidx.credentials:credentials", version.ref = "androidx-credentials" }
129129
androidx-credentials-play-services-auth = { module = "androidx.credentials:credentials-play-services-auth", version.ref = "androidx-credentials-play-services-auth" }
130130
androidx-emoji2-views = { module = "androidx.emoji2:emoji2-views", version.ref = "androidx-emoji2-views" }
131-
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "androidx-fragment-ktx" }
131+
androidx-fragment-compose = { module = "androidx.fragment:fragment-compose", version.ref = "androidx-fragment" }
132+
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "androidx-fragment" }
132133
androidx-glance-appwidget = { module = "androidx.glance:glance-appwidget", version.ref = "androidx-glance-appwidget" }
133134
androidx-glance-appwidget-testing = { module = "androidx.glance:glance-appwidget-testing", version.ref = "androidx-glance-appwidget" }
134135
androidx-glance-material3 = { module = "androidx.glance:glance-material3", version.ref = "androidx-glance-appwidget" }

0 commit comments

Comments
 (0)