Android Weekly Notes #462
2021-06-14 本文已影响0人
圣骑士wind
Android Weekly Issue #462
How to parse JSON in Android using Kotlin
原来不用第三方库解析JSON也挺容易的, 就用JSONTokener
就行.
Android Vitals - Tap Response Time
Py对点击响应时间的研究.
Handling back presses in Jetpack Compose
关于Compose如何处理back.
首先介绍了导航库的基本实现.
一个简单的单向数据流:
abstract class DetailViewModel : ViewModel() {
abstract val state: StateFlow<DetailState>
abstract fun onAction(action: Action)
}
如果我们想获取back的action来进行处理呢?
如果是覆写Activity.onBackPressed()
可能会有点丑. 不是很推荐, 见官方文档.
Compose有这么一个BackHandler composable (added in Activity Compose in 1.3.0-alpha02):
源码是这样:
@Composable
public fun BackHandler(enabled: Boolean = true, onBack: () -> Unit) {
// Safely update the current `onBack` lambda when a new one is provided
val currentOnBack by rememberUpdatedState(onBack)
// Remember in Composition a back callback that calls the `onBack` lambda
val backCallback = remember {
object : OnBackPressedCallback(enabled) {
override fun handleOnBackPressed() {
currentOnBack()
}
}
}
// On every successful composition, update the callback with the `enabled` value
SideEffect {
backCallback.isEnabled = enabled
}
val backDispatcher = checkNotNull(LocalOnBackPressedDispatcherOwner.current) {
"No OnBackPressedDispatcherOwner was provided via LocalOnBackPressedDispatcherOwner"
}.onBackPressedDispatcher
val lifecycleOwner = LocalLifecycleOwner.current
DisposableEffect(lifecycleOwner, backDispatcher) {
// Add callback to the backDispatcher
backDispatcher.addCallback(lifecycleOwner, backCallback)
// When the effect leaves the Composition, remove the callback
onDispose {
backCallback.remove()
}
}
}
具体使用就很简单了:
@Composable
fun Detail(
state: DetailState,
onBack: () -> Unit,
) {
BackHandler(onBack = onBack)
Scaffold(
topBar = { DetailTopAppBar(onBack) }
) {
// detail content
}
}
Animate as State and Animation Specs
关于Compose的动画选择和实现.
Compose, Camera and Canvas
这人的第二周挑战居然搞了一个番茄钟.
Android Runtime — How Dalvik and ART work?
上期有一个视频: https://www.youtube.com/watch?v=0J1bm585UCc
这应该是配套文章.
Assign multiple styles to a single View in Android
给View叠加多个style.
貌似只适用于自定义View.
Using DataStore With Kotlin Serialization
DataStore + Kotlin serialization.
How to Pull Request..
关于PR的种种.