send data to fragment from activity android


This Content is from Stack Overflow. Question asked by scorpions78

I´m trying to send data user to fragment in kotlin.

I have a volley class that connect to mysql db with endpoint and return data login ok.

In my fragment i have a companion object with function and in this block i can show my data ok. In my onCreateView function if i try to set text to my EditText this field it´s empty and if i do a log my data it´s null. I don´t know that i´m doing wrong. I´m very novel with kotlin and android.

In MainActivity i have a login function:

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,
                    // set parameter to activity
                    intent.putExtra("name", name)
                    intent.putExtra("address", address)
                    intent.putExtra("email", email)
                    intent.putExtra("phone", phone)
                    intent.putExtra("password", password)

                    Toast.makeText(applicationContext, "Logged ok", Toast.LENGTH_SHORT).show()
            } catch (e: JSONException) {
        Response.ErrorListener { println("volley Error. Not connection") }) {
        override fun getParams(): Map<String, String>? {
            val params: MutableMap<String, String> = HashMap()
            params["action"] = "login"
            params["email"] = email
            params["password"] = password
            return params

This function instaciate my class volley and send all data. Init my activity ProfileActivity and send data. In my ProfileActivity i send data to my companionObject from Fragment

        override fun onCreate(savedInstanceState: Bundle?) {
            // pager show content
            var pager = findViewById<ViewPager2>(
            // tabLayout it´s TabsMenu
            var tabLayout = findViewById<TabLayout>(
            // Personaliced Adaprter
            pager.adapter = MyAdapter(supportFragmentManager, lifecycle)

            TabLayoutMediator(tabLayout, pager) { tab, position ->
                tab.text = tabTitle[position]

            val profileName = intent.getStringExtra("name")
            val profileAddress = intent.getStringExtra("address")
            val profileEmail = intent.getStringExtra("email")
            val profilePhone = intent.getStringExtra("phone")
            val profilePassword = intent.getStringExtra("password")

            if (profileName != null && profileAddress != null
                && profileEmail != null && profilePhone != null
                && profilePassword != null) {

                ProfileFragment.Companion.newInstance(profileName, profileAddress, profileEmail,
                                            profilePhone, profilePassword)

And in my fragment i have my companion object:

companion object {
        fun newInstance(param1:String, param2:String, param3:String, param4:String, param5:String) =
            ProfileFragment().apply {
                arguments = Bundle().apply {
                    name = param1
                    address = param2
                    userEmail = param3
                    phone = param4
                    password = param5

In this block i can show my data, but if i tray to set text in EditText in function onCreateView it´s null.

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, true)
        val bundle = arguments

        editUserName = view.findViewById<EditText>(
        userAddresEdit = view?.findViewById<EditText>(
        userEmailEdit = view?.findViewById<EditText>(
        userPhoneEdit = view?.findViewById<EditText>(
        userPasswordEdit = view?.findViewById<EditText>(


        Log.d(TAG, "entry: "+name)

        return view

What i´m doing bad? anybody can help me?

Thanks and sorry for my bad english


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?