![](/img/trans.png)
[英]Keyboard with Jetpack Compose was broken by Compose beta-01 (ViewTreeLifecycleOwner not found from DecorView)
[英]Animations and Effects broken on Keyboard written in Jetpack Compose after upgrading to beta-01
在将完全用 Jetpack Compose 编写的键盘从 Compose 版本alpha-11
更新到beta-01
后,我遇到了一个问题。
在升级之前,UI 运行良好,正如您所期望的那样。 波纹显示得很好。 升级后动画和效果(如按下按钮)无法正确播放(波纹效果似乎卡住了)。 看一看:
这是所需的行为以及版本升级之前的样子:
注意: ComposeKeyboardView
之外的相同代码运行良好。 此外,在 Jetpack Compose alpha-11
和beta-01
之前,相同的代码运行良好。 我不确定这是否是一个错误,或者我是否可以自己解决问题。 我感谢任何帮助或提示以恢复所需的行为。
您可以使用以下代码重现该问题:
键盘.kt
@Composable
fun Keyboard() {
Column(
Modifier
.fillMaxWidth()
.height(200.dp)
.background(Color.Gray),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(color = Color.Black, text = "This should resemble a keyboard")
Button(modifier = Modifier.width(250.dp),onClick = { }) {
Text(text = "A Button")
}
}
}
ComposeKeyboardView.kt
class ComposeKeyboardView constructor(
context: Context,
) : AbstractComposeView(context) {
@Composable
override fun Content() {
Keyboard()
}
}
输入法服务.kt
class IMEService : InputMethodService(), LifecycleOwner, ViewModelStoreOwner,
SavedStateRegistryOwner {
override fun onCreateInputView(): View {
val view = ComposeKeyboardView(this)
window!!.window!!.decorView.let { decorView ->
ViewTreeLifecycleOwner.set(decorView, this)
ViewTreeViewModelStoreOwner.set(decorView, this)
ViewTreeSavedStateRegistryOwner.set(decorView, this)
}
return view
}
//Lifecycle Methods
private var lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}
private fun handleLifecycleEvent(event: Lifecycle.Event) =
lifecycleRegistry.handleLifecycleEvent(event)
override fun onCreate() {
super.onCreate()
savedStateRegistry.performRestore(null)
handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
}
override fun onDestroy() {
super.onDestroy()
handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
}
//ViewModelStore Methods
private val store = ViewModelStore()
override fun getViewModelStore(): ViewModelStore = store
//SaveStateRegestry Methods
private val savedStateRegistry = SavedStateRegistryController.create(this)
override fun getSavedStateRegistry(): SavedStateRegistry = savedStateRegistry.savedStateRegistry
不要忘记将 IMEService 添加到您的AndroidManifest.xml
:
<application>
[...]
<service
android:name=".IMEService"
android:label="Example Comopose IME"
android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data
android:name="android.view.im"
android:resource="@xml/method" />
</service>
</application>
应用程序\build.gradle
[...]
dependencies {
def compose_version = "1.0.0-beta01"
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation "androidx.activity:activity-compose:1.3.0-alpha03"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'
}
编辑: compose-beta02 中的相同问题
我下载了您的代码并在 Compose 的错误跟踪器中看到了您的评论。 看起来如果您在IMEService
class 中执行以下操作,则 animation 可以工作。
override fun onCreate() {
super.onCreate()
savedStateRegistry.performRestore(null)
handleLifecycleEvent(Lifecycle.Event.ON_RESUME) // << here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.