简体   繁体   English

如何在应用程序中跨多个片段共享数据?

[英]How can i share data across multiple fragments in application?

I am working on an application where im taking input from users across multiple fragments under same activity, and then i am sending that data to firebase now how can i achieve that using shared viewmodel, if possible please guide me how can i achieve that, I am completely new to android.我正在开发一个应用程序,在该应用程序中,我在同一活动下跨多个片段从用户那里获取输入,然后我将该数据发送到 firebase 现在我如何使用共享视图模型来实现这一点,如果可能的话,请指导我如何实现这一点,我我对 android 完全陌生。 Thank You谢谢你

Can you please elaborate and share some code?您能否详细说明并分享一些代码? It is very difficult to understand what your question is.很难理解你的问题是什么。

I'm assuming you want to share data between fragments.我假设您想在片段之间共享数据。 One way to do that is through ViewModels一种方法是通过 ViewModels

Specifically, you'll need a view model like this:具体来说,您需要这样的视图 model :

class SharedViewModel : ViewModel() {
    val selected = MutableLiveData<Item>()

    fun select(item: Item) {
        selected.value = item
    }
}

Then you have your fragments:然后你有你的片段:

class SharedViewModel : ViewModel() {
    val selected = MutableLiveData<Item>()

    fun select(item: Item) {
        selected.value = item
    }
}

class ListFragment : Fragment() {

    private lateinit var itemSelector: Selector

    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() {

    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
        })
    }
}

Notice that both fragments retrieve the activity that contains them.请注意,两个片段都检索包含它们的活动。 That way, when the fragments each get the ViewModelProvider, they receive the same SharedViewModel instance, which is scoped to this activity.这样,当每个片段都获得 ViewModelProvider 时,它们会收到相同的 SharedViewModel 实例,该实例的范围仅限于此活动。

If you want to know more about this, refer to: https://developer.android.com/topic/libraries/architecture/viewmodel如果您想了解更多相关信息,请参阅: https://developer.android.com/topic/libraries/architecture/viewmodel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 java 中跨片段实现共享视图模型? - How can i implement shared viewmodel across fragments in java? 如何在多个Axis2 Web服务客户端之间共享代理对象? - how can I share proxy objects across multiple Axis2 web service clients? 多个片段能否共享一个 XML 布局 - Can multiple fragments share a single XML layout 如何跨Tomcat的多个实例共享静态变量数据? - How to share static variable data across multiple instances of Tomcat? 如何在同一个父对象的多个实例之间共享数据? - How to share data across multiple instances with a common parent? 如何在多个片段之间共享MediaPlayer对象? - How to share a MediaPlayer object between multiple fragments? 如何在片段中共享从套接字接收到的数据? - How to share received data from socket in fragments? 如何在 android 中的片段和活动之间共享数据 - How to share data between fragments and activities in android 如何使用 ViewModel 在 Fragment 之间共享数据 - How to share the data between Fragments using ViewModel 我如何使用interface.b共享数据黑白片段,我想在片段2的文本视图中单击打开按钮单击片段1中的编辑文本数据 - How can i share data b/w fragments using interface.i want the on button click Edit Text's data in fragment1 to be shown in the text view of fragment2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM