So I have an activity which runs a fragment like this:
private fun replaceFragment(fragment: Fragment, tag: String){
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.frame_layout, Home())
fragmentTransaction.commit()
}
The Fragment looks something like this:
package com.example.safercan
import android.content.ClipData
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class Home : Fragment() {
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val view: View = inflater.inflate(R.layout.fragment_home, container, false)
return view
}
companion object {
@JvmStatic
fun newInstance(param1: String, param2: String) =
Home().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
Inside the Fragment.xml is a EditText which i want to get the content from inside the activity, something like this:
fun get_text(){
setContentView(R.layout.fragment_home)
val text_field = findViewById<EditText>(R.id.editText)
println(text_field.text)}
But anytime I try to get the EditText.text it shows the same result. It does not matter if I change the actual content of the EditText. It is always the default text.
So my Question: How can i get the actual (updated) content of the fragment Edittext from my Activity?
Preparo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.