NiceToMytyuk
NiceToMytyuk

Reputation: 4277

How to pass data from one fragment to another in kotlin?

I have a ViewPager2 in my Android app, i'm new to Kotlin and Fragments, in the first tab in the fragment i have a button which create a record in the database and returns the id of the created row, then i programmatically switch to another tab with another fragment with some fields to be filled with the data from the row created in fragment 1.

At this point i have to get the id i get in fragment 1 in fragment 2 to call my function which return data based on id from my database.

But how can i pass the id i get in fragment 1 to fragment 2?

Here is my Fragment1:

class ElencoFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_elenco, container, false)
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ...
        btnNuovo.setOnClickListener {
            corpoViewModel.insertTestata( // inserting data to database and get the id back
                Testata(
                    "",
                    tipo,
                    "",
                    "",
                    data,
                    true
                )
            ).observe(viewLifecycleOwner) {
                Toast.makeText(requireContext(), it.toString(), Toast.LENGTH_LONG).show() // here i have the id
            }
            viewPager.setCurrentItem(1, false) // changing the PageView to Tab 2 where i have another fragment with the fields to be filled
    }
}

Upvotes: 0

Views: 816

Answers (2)

Gabriel Muniz
Gabriel Muniz

Reputation: 21

To pass your data you can use the bundle of fragment,

Bundle().apply {
        putInt(KEY, imageDrawable)
    }

Upvotes: 0

lpizzinidev
lpizzinidev

Reputation: 13289

A simple approach could be to store a public variable in your parent Activity and modify it to its Fragment children.

First, declare a global variable in your parent Activity

var selectedId = -1

Then, you can access it in your fragments with (Change ParentActivity with the real name of your activity):

(activity as ParentActivity).selectedId = newValue  // Setter

val id = (activity as ParentActivity).selectedId    // Getter

So, based on the code of your first fragment you should assign the id value with

btnNuovo.setOnClickListener {
            corpoViewModel.insertTestata( // inserting data to database and get the id back
                Testata(
                    "",
                    tipo,
                    "",
                    "",
                    data,
                    true
                )
            ).observe(viewLifecycleOwner) {
                (activity as ParentActivity).selectedId = it
            }

And then retrieve it in your second fragment, as explained above.

Upvotes: 1

Related Questions