我想從 viewModel 觀察我的片段內的資料,但 Android Studio 不斷觸發此警告。有人可以幫助解決這個問題嗎?這個問題是否與 Android Studio Bumbleblee 的更新有關?

uj5u.com熱心網友回復:
當你寫
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
// {code to collect from viewModel}
}
}
這repeatOnLifecycle是一個擴展LifecycleOwner——在這里,你隱式使用this——即片段的生命周期,最重要的不是片段視圖生命周期。
如檔案中所見,您應該明確使用 using viewLifecycleOwner.repeatOnLifecycle,這正是 Lint 檢查告訴您使用的內容:
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
// {code to collect from viewModel}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428327.html
標籤:安卓 安卓工作室 科特林 kotlin 协程 安卓生命周期
