activity 和 fragment 之间无法共享数据,无法通信
Data cannot be shared between activity and fragment and cannot communicate
无法在activity和片段之间共享数据,无法通信
Activity无法观察片段数据
Communicating with fragments
视图模型
class BlankViewModel : ViewModel() {
private val mutableSelectedItem = MutableLiveData<String?>()
val onCallbackToDestroyFragment: LiveData<String?> = mutableSelectedItem
fun selectItem(tag: String?) {
mutableSelectedItem.value = tag
}
}
2 个片段
class BlankFragment : Fragment() {
companion object {
fun newInstance() = BlankFragment()
}
private lateinit var viewModel: BlankViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewModel = ViewModelProvider(this).get(BlankViewModel::class.java)
viewModel.selectItem("data")
return inflater.inflate(R.layout.blank_fragment, container, false)
}
}
3 Activity
class MainActivity : AppCompatActivity() {
private lateinit var binding:ActivityMainBinding
private val viewModel by lazy {
ViewModelProvider(this).get(BlankViewModel::class.java)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
supportFragmentManager.beginTransaction()
.add(binding.fl.id, BlankFragment()).commit()
viewModel.onCallbackToDestroyFragment.observe(this , Observer {
Log.e("观察到了吗","Did you observe the data?")
})
}
}
控制台没有数据
这是因为您正在片段中创建 ViewModel 的新实例。
如果您希望 activity 和片段共享您必须使用的同一 ViewModel 实例。
private lateinit var viewModel: BlankViewModel by activityViewModels()
并删除片段中的提供者声明
.. 或者您可以创建一个 customViewModel 工厂并共享它。
无法在activity和片段之间共享数据,无法通信 Activity无法观察片段数据 Communicating with fragments
视图模型
class BlankViewModel : ViewModel() {
private val mutableSelectedItem = MutableLiveData<String?>()
val onCallbackToDestroyFragment: LiveData<String?> = mutableSelectedItem
fun selectItem(tag: String?) {
mutableSelectedItem.value = tag
}
}
2 个片段
class BlankFragment : Fragment() {
companion object {
fun newInstance() = BlankFragment()
}
private lateinit var viewModel: BlankViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewModel = ViewModelProvider(this).get(BlankViewModel::class.java)
viewModel.selectItem("data")
return inflater.inflate(R.layout.blank_fragment, container, false)
}
}
3 Activity
class MainActivity : AppCompatActivity() {
private lateinit var binding:ActivityMainBinding
private val viewModel by lazy {
ViewModelProvider(this).get(BlankViewModel::class.java)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
supportFragmentManager.beginTransaction()
.add(binding.fl.id, BlankFragment()).commit()
viewModel.onCallbackToDestroyFragment.observe(this , Observer {
Log.e("观察到了吗","Did you observe the data?")
})
}
}
控制台没有数据
这是因为您正在片段中创建 ViewModel 的新实例。
如果您希望 activity 和片段共享您必须使用的同一 ViewModel 实例。
private lateinit var viewModel: BlankViewModel by activityViewModels()
并删除片段中的提供者声明 .. 或者您可以创建一个 customViewModel 工厂并共享它。