Skip to content

Commit b90686d

Browse files
jbw0033jbwoods
andauthored
Refactor Fragment-in-Compose snippet to use AndroidFragment (#676)
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. Co-authored-by: jbwoods <jbwoods@google.com>
1 parent 3759291 commit b90686d

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"
@@ -141,7 +141,8 @@ androidx-datastore-preferences-core = { group = "androidx.datastore", name = "da
141141
androidx-datastore-preferences-rxjava2 = { module = "androidx.datastore:datastore-preferences-rxjava2", version.ref = "datastorePreferencesRxjava2" }
142142
androidx-datastore-preferences-rxjava3 = { module = "androidx.datastore:datastore-preferences-rxjava3", version.ref = "datastorePreferencesRxjava3" }
143143
androidx-emoji2-views = { module = "androidx.emoji2:emoji2-views", version.ref = "androidx-emoji2-views" }
144-
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "androidx-fragment-ktx" }
144+
androidx-fragment-compose = { module = "androidx.fragment:fragment-compose", version.ref = "androidx-fragment" }
145+
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "androidx-fragment" }
145146
androidx-glance-appwidget = { module = "androidx.glance:glance-appwidget", version.ref = "androidx-glance-appwidget" }
146147
androidx-glance-appwidget-testing = { module = "androidx.glance:glance-appwidget-testing", version.ref = "androidx-glance-appwidget" }
147148
androidx-glance-material3 = { module = "androidx.glance:glance-material3", version.ref = "androidx-glance-appwidget" }

0 commit comments

Comments
 (0)