本文介绍了使用 Android 导航组件中的 navigationUp 传递数据/包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我如何获取前一个片段中的数据.我可以使用

从 Frag B 导航到 Frag A

解决方案

根据 developer.android.com,您可以将 common 用于要共享数据的片段 ViewModel 使用他们的活动范围.

以下是步骤:

  1. 创建将保留数据的视图模型:
class SharedViewModel : ViewModel() {val dataToShare = MutableLiveData()有趣的更新数据(数据:字符串){dataToShare.value = 数据}}
  1. 观察 Fragment1 中的数据变化:
class Fragment1 : Fragment() {private lateinit var viewModel: SharedViewModel覆盖 fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(视图,savedInstanceState)viewModel = ViewModelProviders.of(activity!!).get(SharedViewModel::class.java)viewModel.dataToShare.observe(this, Observer { dataFromFragment2 ->//对数据做一些事情})}}
  1. 更新 Fragment2 中的数据,如果您正确处理导航,现在您应该能够接收 Fragment1 上的数据更改:
class Fragment2 : Fragment() {private lateinit var viewModel: SharedViewModel覆盖 fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(视图,savedInstanceState)viewModel = ViewModelProviders.of(activity!!).get(SharedViewModel::class.java)updateDataButton.setOnClickListener { v ->viewModel.updateData("fragment1 的新数据")}}}

希望回答有帮助.

I found the question but does not have solution in code

I want to have data when backpress/manual back happens. I am using navigateUp() to go back. How can I pass data to previous fragment? navigateUp() does not have any facility to pass data to previous fragment. Even I did not find solution using Safe Args. It's passing data forward. I want to have in backward Frad B -> Frag A.

My code to go back to previous fragment

Navigation.findNavController(view).navigateUp()

My question is, How can i get data in previous fragment. I can navigate to Frag A from Frag B using

解决方案

According to developer.android.com, you can use common for fragments where you want to share data ViewModel using their activity scope.

Here are steps:

  1. Create view model which will keep the data:
class SharedViewModel : ViewModel() {
    val dataToShare = MutableLiveData<String>()

    fun updateData(data: String) {
        dataToShare.value = data
    }
}
  1. Observe data changes in Fragment1:
class Fragment1 : Fragment() {

    private lateinit var viewModel: SharedViewModel

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProviders.of(activity!!).get(SharedViewModel::class.java)
        viewModel.dataToShare.observe(this, Observer<String> { dataFromFragment2 ->
            // do something with data
        })
    }
}
  1. Update data in Fragment2 and if you're handling navigation properly, now, you should be able to receive data changes on Fragment1:
class Fragment2 : Fragment() {

    private lateinit var viewModel: SharedViewModel

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProviders.of(activity!!).get(SharedViewModel::class.java)

        updateDataButton.setOnClickListener { v ->
            viewModel.updateData("New data for fragment1")
        }
    }
}

I hope answer helps.

这篇关于使用 Android 导航组件中的 navigationUp 传递数据/包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:46