我有一个活动应用程序,仅使用 ui 的可组合项(一个活动,没有片段)。我使用一个视图模型在两个不同的屏幕(可组合项)中保存 ui 的数据。我按照状态文档中的描述在两个屏幕中创建视图模型
@Composable
fun HelloScreen(helloViewModel: HelloViewModel = viewModel())
Run Code Online (Sandbox Code Playgroud)
现在我注意到在第一个屏幕中加载或设置的数据在第二个屏幕中被重置。
我还注意到init{}
每次被调用时viewModel()
都会被调用。这真的是预期的行为吗?
根据该方法的文档,它应该返回现有的 ViewModel 或创建一个新的 ViewModel。
我还看到视图模型是不同的对象。所以viewModel()
总是创建一个新的。但为什么?
有什么想法我可能做错了什么吗?或者我误解了该方法的用法?
更新撰写导航依赖项的版本后,我的应用程序在启动时直接崩溃,出现以下异常:
java.lang.NoSuchMethodError: No static method drawRect-w2WG-Gw$default(Landroidx/compose/ui/graphics/drawscope/DrawScope;JJJFLandroidx/compose/ui/graphics/drawscope/DrawStyle;Landroidx/compose/ui/graphics/ColorFilter;Landroidx/compose/ui/graphics/BlendMode;ILjava/lang/Object;)V in class Landroidx/compose/ui/graphics/drawscope/DrawScope$DefaultImpls; or its super classes (declaration of 'androidx.compose.ui.graphics.drawscope.DrawScope$DefaultImpls' appears in /data/app/~~pBKRwWSGSd6Trycrlz_8bw==/com.example.wunderguard-9YsObT0GJC9oUzrTLizqog==/base.apk)
at androidx.compose.foundation.Background.drawRect(Background.kt:111)
at androidx.compose.foundation.Background.draw(Background.kt:103)
...
Run Code Online (Sandbox Code Playgroud)
当使用旧版本时,我得到了这个问题中描述的异常
你有什么想法,可能导致这个问题的原因是什么?这些是我的依赖项(compose_version = '1.0.0-beta07')
dependencies {
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
implementation "androidx.navigation:navigation-compose:2.4.0-alpha02"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation "androidx.activity:activity-compose:1.3.0-beta01"
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
Run Code Online (Sandbox Code Playgroud)
}
我实现了一个简单的HorizontalPager,它基本上按预期工作。我开发的应用程序适用于一个特定设备,即横向模式下的 8 英寸平板电脑。目前需要滑动超过 50% 的屏幕宽度才能到达下一页/上一页。这是一个非常长的滑动我想减少它以使更改页面更容易......
我尝试了fling 行为,并尝试在偏移量更改时手动更改页面或拦截触摸事件......没有什么真正导致所需的行为。
既然“问题”看起来如此简单,我真的希望我刚刚疏忽了一些事情。你知道我可以尝试什么吗?
android android-viewpager android-jetpack-compose jetpack-compose-accompanist
我设置了一个地理围栏,当设备退出半径时,该地理围栏会按预期触发。然而,地理围栏事件完全是空的。
override fun onReceive(context: Context, intent: Intent) {
val geofencingEvent = GeofencingEvent.fromIntent(intent)
}
Run Code Online (Sandbox Code Playgroud)
这并不奇怪,因为意图也是空的。但是地理围栏触发意图通常从哪里获取数据来帮助 GeofencingEvent.fromIntent() 构建事件?如果意图是空的,会出现什么问题?(没有任何操作,没有额外的内容...意图 uri 如下所示:
intent:#Intent;launchFlags=0x10;component=de.yukiarts.test/.GeofenceBroadcastReceiver;end
Run Code Online (Sandbox Code Playgroud)