Milos
Milos

Reputation: 45

Fragment in Android App (Kotlin) sets values to null

I am working on an App that uses Volley to send requests and Bundle to enable communication between fragments.

This is the part of the code from first fragment:

login.setOnClickListener {
            var data12 = "null"
            var data13 = "null"
            val bundle = Bundle()
            val builder4 = Uri.Builder()
            builder4.scheme("http")
                    .encodedAuthority("18.100.200.300:8001")
                    .appendEncodedPath("records")
                    .appendEncodedPath(id)
            val myUrl4 = builder4.build().toString()

            val JsonObjRequest4 = object : JsonObjectRequest(Request.Method.GET, myUrl4, null,
                    { response4 ->
                        data12 = response4.getJSONObject("presentation").getJSONObject("requested_proof").getJSONObject("revealed_attr_groups").getJSONObject("additionalProp1").getJSONObject("values").getJSONObject("name").getString("raw")
                        data13 = response4.getJSONObject("presentation").getJSONObject("requested_proof").getJSONObject("revealed_attr_groups").getJSONObject("additionalProp1").getJSONObject("values").getJSONObject("email").getString("raw")

                        bundle.putString("name", data12)
                        bundle.putString("email", data13)

                        val fragment2 = Loginsuccess()
                       fragment2.setArguments(bundle)

                        getFragmentManager()?.beginTransaction()?.replace(R.id.content, fragment2)?.commit()
                    },
                    { error4 ->
                        Toast.makeText(context, error4.toString(), Toast.LENGTH_SHORT).show()
                    }) {
                override fun getHeaders(): MutableMap<String, String> {
                    val headers = HashMap<String, String>()
                    headers["accept"] = "application/json; charset=utf-8"
                    headers["Authorization"] = "Bearer xxx"
                    return headers
                }
            }


            // Add the request to the RequestQueue.
            queue.add(JsonObjRequest4)



            Navigation.findNavController(view).navigate(R.id.action_to_loginsuccess)



        }

The second fragment looks like this:

class Loginsuccess : StandardContentFragment() {
    override fun isFunctionBarVisibleByDefault(): Boolean = false

    override fun onCreateContentView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View = inflater.inflate(R.layout.fragment_loginsuccess, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val name = arguments?.getString("name").toString()
        val email = arguments?.getString("email").toString()

        val Header = view.findViewById<TextView>(R.id.hitext)
        val emailtext = view.findViewById<TextView>(R.id.emailtext)

        Header.setText("Hi ${name} !")
        emailtext.setText("Your Email is ${email}.")


    }

}

However, the problem is when I press the button, GET request is sent and those data are than sent to second fragment where Name and Email should be shown. In my case even before I get my Name and Email, I get:

Hi null ! Your Email is null.

After some time real values appear and overlap previous ones.

How can I fix this?

Upvotes: 0

Views: 331

Answers (1)

mightyWOZ
mightyWOZ

Reputation: 8335

It seems you are loading the same Fragment twice, first its loaded with null values when you write

Navigation.findNavController(view).navigate(R.id.action_to_loginsuccess)

After this your network call finishes and the fragment is loaded again, but this time with arguments by following line

 getFragmentManager()?.beginTransaction()?.replace(R.id.content, fragment2)?.commit()

to fix this remove the Navigation.findNavController(view).navigate and it should work as expected

Upvotes: 1

Related Questions