Lenin Bakhara
Lenin Bakhara

Reputation: 349

Pass data between fragments in kotlin android studio

I want to pass some data from PowerofMind to wishlist fragment but I encountered with some error.

This Activity from where data has to be transferred

      wish?.setOnClickListener({
            val name = "Power of Subconcoius Mind"
            val intent = Intent(this@PowerofMind, WishlistFragment::class.java)
            intent.putExtra("Book: ", name)
            startActivity(intent)
            Toast.makeText(this, "Added to WishList", Toast.LENGTH_SHORT).show()
        })

I want to show data in this activity as



class WishlistFragment : Fragment() {
    var result: TextView? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_wishlist, null)
        result =view.findViewById(R.id.list1)
        val name = activity?.intent?.getStringExtra("Book: ")
        list1.text = "Book: $name"
        return view
    }
}

But there is an error on Intent. Please Help

Upvotes: 0

Views: 8864

Answers (3)

Kuruchy
Kuruchy

Reputation: 1370

In your WishlistFragment you are creating a new Intent instead of getting the one provided by the activity.

As you are using Kotlin, you can directly use intent instead of getIntent(). You could use also the synthetic advantage of kotlin, and drop that findViewById.

And as a tip, do not use string concatenation ;)

Your fragment would look something like the following:

class WishlistFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_wishlist, null)
    val name = activity?.intent?.getStringExtra("Book: ")
    many.text = "Book: $name"
}

Upvotes: 0

Ankit Tale
Ankit Tale

Reputation: 2004

Here is how you can pass data between fragment in kotlin using Parcelable class:

on Button Click:

     override fun onClick(v: View?) {
        firstName = editTextName!!.text.toString()
        lastName = editTextLast!!.text.toString()
        Toast.makeText(context, firstName, Toast.LENGTH_SHORT).show()
//        val viewFragment = ViewFragment()
//        val transaction = fragmentManager.beginTransaction()
//        transaction.replace(R.id.fragmentContainer, viewFragment)
//        transaction.commit()

        var details = Details(firstName!!, lastName!!)
        val viewFragment = ViewFragment()
        val bundle = Bundle()
        bundle.putParcelable(KEY_PARSE_DATA, details)
        viewFragment.setArguments(bundle)
        val transaction = fragmentManager.beginTransaction()
        transaction.replace(R.id.fragmentContainer, viewFragment)
        transaction.commit()
    }

Here is an parcel class how to handle data

     @Parcelize
class Details(val firstName: String, val lastName: String) : Parcelable

on another fragment

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view: View = inflater!!.inflate(R.layout.fragment_view, container, false)

        textViewName = view.findViewById(R.id.text_name_another) as TextView
        textViewLastName = view.findViewById(R.id.text_surname_another) as TextView

        val bundle = arguments
        if (bundle != null) {
            val details = bundle.getParcelable<Details>(KEY_PARSE_DATA)
            textViewName!!.setText(details.firstName)
            textViewLastName!!.setText(details.lastName)
        }


        return view
    }

Currently I don't know if this required or not in kotlin in app gradle (Check this before used)

androidExtensions {
    experimental = true
}

Upvotes: 1

Link182
Link182

Reputation: 839

Here is an example how to instantiate a fragment with factory method:

companion object {
    private const val MY_DATA_KEY = "my_data"
    private const val ANOTHER_DATA_KEY = "another_data"
    fun newInstance(mySerializableData: Any, anotherData: Int) = MyFragment().apply {
        //bundleOf() is an exstension method from KTX https://developer.android.com/kotlin/ktx
        arguments = bundleOf(MY_DATA_KEY to mySerializableData, ANOTHER_DATA_KEY to anotherData)
    }
}

Upvotes: 2

Related Questions