SharedViewmodel 的实例永远不会消亡吗?
Does an instance of a SharedViewmodel never dies?
我有一个应用有一个主 activity 并且片段依赖于它,所以这是正常的。
现在,我的 10 个片段中有两个需要通信,我使用此处给出的示例
https://developer.android.com/topic/libraries/architecture/viewmodel.html#sharing
class SharedViewModel : ViewModel() {
val selected = MutableLiveData<Item>()
fun select(item: Item) {
selected.value = item
}
}
class MasterFragment : Fragment() {
private lateinit var itemSelector: Selector
// Use the 'by activityViewModels()' Kotlin property delegate
// from the fragment-ktx artifact
private val model: SharedViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
itemSelector.setOnClickListener { item ->
// Update the UI
}
}
}
class DetailFragment : Fragment() {
// Use the 'by activityViewModels()' Kotlin property delegate
// from the fragment-ktx artifact
private val model: SharedViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
model.selected.observe(viewLifecycleOwner, Observer<Item> { item ->
// Update the UI
})
}
}
现在,如果 MasterFragment 和 DetailFragment 都死了(两者都死了 popBackStack()),视图模型的那个实例会保持活动状态直到我完成包含这个 Fragment 的 MainActivity 吗?因为现在我不再需要那个 viewmodel 实例,但是根据文档所说,这个实例将从包含这些片段的 Activity 中保留
这不是我想要在片段之间进行通信的方式,因为现在该视图模型的新实例将与我创建的过去相同,我的意思是,它将重用我使用过的实例已经弹出的片段,我将需要额外处理删除或重置此视图模型中的所有数据,而不是获取新的新视图模型。
它是这样工作的还是当堆栈中不再有依赖于它的片段时该实例自动死亡?
Now, if MasterFragment and DetailFragment dies (both does a popBackStack()) does that instance of the viewmodel keep active untill I finish the MainActivity containing this Fragments ?
正确。虽然碰巧只有两个片段使用它,但 ViewModel
的范围是 activity.
I mean, it will reuse the instance that I used with the already poped fragments, in which I will need to extra handling a deletion or reset of all the data inside this viewmodel instead of getting a new fresh viewmodel.
那么也许你不应该使用 activityViewModels()
。例如,您可以将这两个片段隔离到一个嵌套的导航图中,并设置一个作用域为该图的视图模型。
Does it works this way or that instance automatically dies when no fragments depending on it are in the stack anymore ?
ViewModel
系统不知道什么是什么不是 "depending on it"。它全部基于提供它的 ViewModelStore
和 the ViewModelStoreOwner
。 activityViewModels()
使用 activity 作为 ViewModelStoreOwner
,因此 ViewModelStore
中的视图模型与 activity 相关联。
我有一个应用有一个主 activity 并且片段依赖于它,所以这是正常的。
现在,我的 10 个片段中有两个需要通信,我使用此处给出的示例
https://developer.android.com/topic/libraries/architecture/viewmodel.html#sharing
class SharedViewModel : ViewModel() {
val selected = MutableLiveData<Item>()
fun select(item: Item) {
selected.value = item
}
}
class MasterFragment : Fragment() {
private lateinit var itemSelector: Selector
// Use the 'by activityViewModels()' Kotlin property delegate
// from the fragment-ktx artifact
private val model: SharedViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
itemSelector.setOnClickListener { item ->
// Update the UI
}
}
}
class DetailFragment : Fragment() {
// Use the 'by activityViewModels()' Kotlin property delegate
// from the fragment-ktx artifact
private val model: SharedViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
model.selected.observe(viewLifecycleOwner, Observer<Item> { item ->
// Update the UI
})
}
}
现在,如果 MasterFragment 和 DetailFragment 都死了(两者都死了 popBackStack()),视图模型的那个实例会保持活动状态直到我完成包含这个 Fragment 的 MainActivity 吗?因为现在我不再需要那个 viewmodel 实例,但是根据文档所说,这个实例将从包含这些片段的 Activity 中保留
这不是我想要在片段之间进行通信的方式,因为现在该视图模型的新实例将与我创建的过去相同,我的意思是,它将重用我使用过的实例已经弹出的片段,我将需要额外处理删除或重置此视图模型中的所有数据,而不是获取新的新视图模型。
它是这样工作的还是当堆栈中不再有依赖于它的片段时该实例自动死亡?
Now, if MasterFragment and DetailFragment dies (both does a popBackStack()) does that instance of the viewmodel keep active untill I finish the MainActivity containing this Fragments ?
正确。虽然碰巧只有两个片段使用它,但 ViewModel
的范围是 activity.
I mean, it will reuse the instance that I used with the already poped fragments, in which I will need to extra handling a deletion or reset of all the data inside this viewmodel instead of getting a new fresh viewmodel.
那么也许你不应该使用 activityViewModels()
。例如,您可以将这两个片段隔离到一个嵌套的导航图中,并设置一个作用域为该图的视图模型。
Does it works this way or that instance automatically dies when no fragments depending on it are in the stack anymore ?
ViewModel
系统不知道什么是什么不是 "depending on it"。它全部基于提供它的 ViewModelStore
和 the ViewModelStoreOwner
。 activityViewModels()
使用 activity 作为 ViewModelStoreOwner
,因此 ViewModelStore
中的视图模型与 activity 相关联。