Android Weekly Notes

Android Weekly Notes #449

2021-02-09  本文已影响0人  圣骑士wind

Android Weekly Issue #449

Android Kotlin Analytics Made Simple!

这居然还有一个awesome-analytics的list:
https://github.com/onurakpolat/awesome-analytics/blob/master/README.md

讲了analytics库的包装和注入.

Avoid backing properties for LiveData and StateFlow

LiveData的这种写法, 算是比较典型:

class MyViewModel: ViewModel() {
   val loading: LiveData<Boolean>
       get() = _loading
   private val _loading = MutableLiveData<Boolean>()
}

这种命名方法甚至在官方的convention里也有:
https://kotlinlang.org/docs/reference/coding-conventions.html#property-names

建议的方式是使用ViewModel接口或者抽象类.

abstract class MyViewModel: ViewModel() {
   abstract val loading: LiveData<Boolean>
}
class MyViewModelImpl: MyViewModel() {
   override val loading = MutableLiveData<Boolean>()
}

Migrate from LiveData to StateFlow and SharedFlow

用StateFlow和SharedFlow替换LiveData.

LiveData的问题:

验证可行性, 例子:

My MutableStateFlow Didn’t Emit!

一个MutableStateFlow使用时遇到的问题.

Choose Wisely: 5 Ways To Show A Message In Android

五种显示信息的方式, 该如何选择.

这个流程图很好:
https://vladsonkin.com/wp-content/uploads/2021/01/show-message-android-diagram.jpg

Code

上一篇下一篇

猜你喜欢

热点阅读