Home > OS >  send parameter from activity to Fragment kotlin
send parameter from activity to Fragment kotlin

Time:07-22

i´m traying to send parameter to fragment. I´m using Volley Class to get data from my db with mysql. I´m transform my response in jsonObject and create my arguments:

val stringRequest: StringRequest = object : StringRequest(
            Method.POST, EndPoint.URL,
            Response.Listener { response ->
                try {
                    val jsonObject = JSONObject(response)

                    if (jsonObject != null) {
                        val dataJson: JSONObject = jsonObject.getJSONObject("user")
                        val name = dataJson.getString("name")
                        val address = dataJson.getString("address")
                        val email = dataJson.getString("email")
                        val phone = dataJson.getString("phone")
                        val password = dataJson.getString("password")

                        val intent = Intent(this, ProfileActivity::class.java)
                        // set parameter to activity
                        val bundle = Bundle()
                        bundle.putString("name", name)
                        bundle.putString("address", address)
                        bundle.putString("email", email)
                        bundle.putString("phone", phone)
                        bundle.putString("password", password)
                        intent.putExtras(bundle)
                        Toast.makeText(applicationContext, "Logged ok", Toast.LENGTH_SHORT).show()
                        startActivity(intent)


                    }
                }

In my Profile activity i´m receiving my parameters:

name = intent.getStringExtra("name").toString()
address = intent.getStringExtra("address").toString()
userEmail = intent.getStringExtra("email").toString()
phone = intent.getStringExtra("phone").toString()
password = intent.getStringExtra("password").toString()

I/ContentValues: name: david
I/ContentValues: userEmail: [email protected]
I/ContentValues: phone: null
I/ContentValues: pass: 123456

But i´m setting my values in EditText but i can´t show it when execute my app. In my fragment i´m doing:

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

        val editUserName = view.findViewById<EditText>(R.id.userName)
        var userAddresEdit = view.findViewById<EditText>(R.id.userAddress)
        var userEmailEdit = view.findViewById<EditText>(R.id.userEmailText)
        var userPhoneEdit = view.findViewById<EditText>(R.id.userPhone)
        var userPasswordEdit = view.findViewById<EditText>(R.id.userPasswordEdit)

        /*  userAddresEdit.setText(userAddressConst.toString())
          userEmailEdit.setText(userEmailConst.toString())
          userPhoneEdit.setText(userPhoneConst.toString())
          userPasswordEdit.setText(passwordConst.toString())*/

        Log.i(TAG, bundle.toString())

        return view
    }

My log show null bundle variable.. Idon´t know i cant pass my data to fragment i´m reading in internet, but i can´t find info for to do this.

I´m using kotlin in all my proyect.

Thanks for readme and sorry for my bad english

Update

profileActivity

ProfileFragment.newInstance(name, address, userEmail, phone, password)

Fargment

companion object {

        @JvmStatic
        fun newInstance(param1:String, param2:String, param3:String, param4:String, param5:String) =
            ProfileFragment().apply {
                arguments = Bundle().apply {
                    name = param1
                }
            }
    }

result

I/ContentValues: name: david
I/ContentValues: userEmail: [email protected]
I/ContentValues: phone: null
I/ContentValues: pass: 123456
I/ContentValues: null

CodePudding user response:

Write code inside your Fragment

companion object {

    @JvmStatic
    fun newInstance(param1: String, param2: String) =
        VideoDetailsFragment().apply {
            arguments = Bundle().apply {

            }
        }
}

then call this from activity like this

TestFragment.newInstance("param value 1","param value 2")

CodePudding user response:

In your profile activity, create another bundle with the data and then when you display the fragment:

ProfileFragment().also { it.arguments = bundle }.show(/*fragment manager*/, /*Tag*/)
  • Related