This application counts up A when C is pressed. Since A is the only one that has changed, I expected the recomposition to be just that. But C is also recomposition.
Here is the code.
ViewModel exposes StateFlow.
class MainViewModel : ViewModel() {
private val _count: MutableStateFlow<Int> = MutableStateFlow(0)
val count: StateFlow<Int> = _count.asStateFlow()
fun increaseCount() {
_count.value++
}
}
Run Code Online (Sandbox Code Playgroud)
CCompose
calls increaseCount()
.
@Composable
fun CountUpScreen(
modifier: Modifier = Modifier,
viewModel: MainViewModel = viewModel(),
) {
val count: Int …
Run Code Online (Sandbox Code Playgroud) “ kotlin-allopen”插件有效,但“ kotlin-noarg”插件无效。我能怎么做?
下面是代码。
build.gradle
buildscript {
ext.kotlin_version = '1.1.3-2'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
classpath "io.realm:realm-gradle-plugin:3.5.0"
}
}
apply plugin: "kotlin-allopen"
apply plugin: "kotlin-noarg"
allOpen {
annotation("sample.AllOpen")
}
noArg {
annotation("sample.NoArg")
invokeInitializers = true
}
Run Code Online (Sandbox Code Playgroud)
app / build.gradle
apply plugin: 'realm-android'
Run Code Online (Sandbox Code Playgroud)
NoArg.kt
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class NoArg
Run Code Online (Sandbox Code Playgroud)
MyApplication.kt
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
Realm.init(this)
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml
<application
android:name=".MyApplication"
Run Code Online (Sandbox Code Playgroud)
SampleEntity.kt
@NoArg
@AllOpen
@RealmClass
data …
Run Code Online (Sandbox Code Playgroud)