たくさんの自由帳
Androidのお話
たくさんの自由帳
投稿日 : | 0 日前
文字数(だいたい) : 1511
alpha 12
から直ってない
beta02
で修正されました。更新手順は、
app/build.gradle
のKotlinバージョン、Composeのバージョンを以下のように変更し、
// Compose関係
composeOptions {
kotlinCompilerVersion '1.4.31'
kotlinCompilerExtensionVersion '1.0.0-beta02'
}
Composeのバージョンを上げて、
Fragment
、AppCompat
のバージョンを1.3
以上にすればこの問題は修正できます。
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "androidx.compose.ui:ui:1.0.0-beta02"
// Tooling support (Previews, etc.)
implementation "androidx.compose.ui:ui-tooling:1.0.0-beta02"
// Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
implementation "androidx.compose.foundation:foundation:1.0.0-beta02"
// Material Design
implementation "androidx.compose.material:material:1.0.0-beta02"
// Integration with observables
implementation "androidx.compose.runtime:runtime-livedata:1.0.0-beta02"
// LayoutInspector
implementation "androidx.compose.ui:ui-tooling:1.0.0-beta02"
// fragment
implementation 'androidx.fragment:fragment-ktx:1.3.1'
// appcompat
implementation 'androidx.appcompat:appcompat:1.3.0-beta01'
}
BottomSheetDialogFragment
のonCreateView
の返り値としてComposeView()
を使うとエラーが出る問題
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from DecorView@b98b3fc[MainActivity]
name | value |
---|---|
Compose | 1.0.0 Beta 1 |
ViewTreeLifecycleOwner
っていうView
からActivity/Fragment
のライフライクルを取得できるやつがあるんですけど、
親Viewを指定してViewTreeLifecycleOwner.get()
を呼ぶとなぜかnullが返ってくる
お好きな方をどうぞ
ViewTreeLifecycleOwner
(とRecomposer
)を指定するandroidx.fragment
をスナップショット版にするViewTreeLifecycleOwner
を指定するCompose
で使ってるRecomposer
を作るのにViewTreeLifecycleOwner
が必要だった模様。
というわけでRecomposer
を作って渡してあげる必要があります
ViewTreeLifecycleOwner.set()
の第二引数はActivity
の場合はActivity
を(this)、
Fragment
の場合はviewLifecycleOwner
を渡してあげてください
class BottomFragmentCompose : BottomSheetDialogFragment() {
@InternalComposeUiApi
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return ComposeView(requireContext()).apply {
ViewTreeLifecycleOwner.set(this, viewLifecycleOwner)
val newRecomposer = AtomicReference(WindowRecomposerFactory.LifecycleAware).get().createRecomposer(rootView)
compositionContext = newRecomposer
setContent {
Text(
text = "BottomSheetDialogFragment + ComposeView",
modifier = Modifier.padding(10.dp)
)
}
}
}
}
Dialog
でComposeView
を使う場合はViewTreeSavedStateRegistryOwner
の指定も必要かも
@InternalComposeUiApi
private fun showDialog() {
Dialog(this).apply {
setContentView(ComposeView(context).apply {
ViewTreeLifecycleOwner.set(this, this@MainActivity)
ViewTreeSavedStateRegistryOwner.set(this, this@MainActivity)
val newRecomposer = AtomicReference(WindowRecomposerFactory.LifecycleAware).get().createRecomposer(this)
compositionContext = newRecomposer
setContent {
Text(
text = "Dialog + ComposeView",
modifier = Modifier.padding(10.dp)
)
}
})
}.show()
}
ついさっき知ったんですけど、build.gradle
からallprojects
が消滅してた。(gradleが6.8になった影響?)
代替として、settings.gradle
にdependencyResolutionManagement
が追加された模様
allprojects {
repositories {
google()
jcenter()
maven { url 'https://androidx.dev/snapshots/builds/7172350/artifacts/repository' } // これを書き足す
}
}
あとはapp
フォルダにあるbuild.gradle
を開いて、androidx.fragment
をアップデートします
dependencies {
implementation "androidx.fragment:fragment:1.4.0-SNAPSHOT"
}
settings.gradle
を開いて書き足す
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
maven { url 'https://androidx.dev/snapshots/builds/7172350/artifacts/repository' }
}
}
あとはapp
フォルダにあるbuild.gradle
を開いて、androidx.fragment
をアップデートします
dependencies {
implementation "androidx.fragment:fragment:1.4.0-SNAPSHOT"
}
dependencyResolutionManagement ってなに
あとソースコード置いておきます
https://github.com/takusan23/BottomFragmentComposeView