Home > Software engineering >  I can't get data in fragment By Using Bundle()
I can't get data in fragment By Using Bundle()

Time:08-08

in recycler view when click item work and clicked and the fragment open successfully but the date can't send or error is show in fragment when call the date in Bundle()

//This Code In RecyclerView

    holder.setItemClickListener(object:IItemClickListenerQuran{
        override fun clickedItem(view: View, position: Int) {
            Toast.makeText(activity, "Click At Quran Sora: "   items[position].name,Toast.LENGTH_SHORT).show()

            val bundle = Bundle()
            bundle.putString("SoraName", items[position].name)

            val fragment = SoraDetailsFragment()
            val activity = view.context as AppCompatActivity
            activity.supportFragmentManager.beginTransaction().replace(R.id.main_Fragment_Continer, fragment).addToBackStack(null).commit()

//This Code In Fragment

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view:View =  inflater.inflate(R.layout.fragment_sora_details, container, false)

    val args = arguments
    val SoraName = args!!.getString("SoraName")
    Log.d("receiver", "SoraName: "   SoraName);

    return view

CodePudding user response:

if you want to pass the data using safeArg, here is the approach enter link description here

CodePudding user response:

You are creating bundle . But you didn't bind the bundle as arguments. Try adding this line.

val bundle = Bundle()
bundle.putString("SoraName", items[position].name)
val fragment = SoraDetailsFragment()
fragment.arguments = bundle // This is where bundle is attached to fragment as arguments
  • Related