Fragment closes by clicking on Textview Kotlin

Asked

Viewed 80 times

1

Save list! Next, I have an Activity where I call a Fragment using

// ...
val manager = supportFragmentManager
val transaction = manager.beginTransaction()

_Fragment = AcvtFragmentView()
val args = Bundle()
args.putBoolean("cal_warning", true)
args.putString("ID", ID)
args.putString("name", kName)
args.putString("email", kEmail)
args.putString("date", date)
args.putString("time", time)
args.putString("keypub", keypub)
args.putString("msg", interpretation)
_Fragment!!.setArguments(args)

transaction.replace(R.id.fragmentview, _Fragment!!)
transaction.addToBackStack(null)
transaction.commit()
// ...

It’s working well, but what happens is that when it opens on the screen of this Activity and I click on any Textview inside this fragment it closes alone and I need to type text in these fields. I’ve been all over the Internet.

I wonder if anyone here could help me with this?

Thanks

Follows the code of Activity Acvtfragmenteview:

class AcvtFragmentView : Fragment() {
// ...
internal var actionCallBack: onActionEventListener? = null

interface onActionEventListener {
    fun dlgActions(id: Int,name: String?,email: String?,datepub: String?,interpretation: String?)
}

override fun onAttach(context: Context) {
    super.onAttach(context)
    mContext = context
    try {
        actionCallBack = context as onActionEventListener
    } catch (e: ClassCastException) {
        throw ClassCastException("$activity must implement onSomeEventListener")
    }
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    mydb = DreamsItemHelper(mContext)
}
override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
    window = inflater.inflate(R.layout.layout_interprete, container, false)

    val ID = arguments!!.getString("ID")
    val dDate = arguments!!.getString("date")
    val tTime = arguments!!.getString("time")
    val keypub = arguments!!.getString("keypub")
    val interp = arguments!!.getString("msg")

    date = window!!.findViewById<View>(R.id.datepubItView) as TextView
    date!!.text = dDate

    val kName = arguments!!.getString("name")
    val kEmail = arguments!!.getString("email")

    val name = window!!.findViewById<View>(R.id.interName) as EditText
    name.setText(kName)
    val email = window!!.findViewById<View>(R.id.interEmail) as EditText
    email.setText(kEmail)
    interpretation = window!!.findViewById<View>(R.id.edtInterpretation) as EditText
    interpretation!!.text = interp

    btnsend = window!!.findViewById<View>(R.id.btnSendToInterpret) as TextView
    if (interp != null) {
        btnsend!!.isEnabled = false
    }

    btnsend!!.setOnClickListener {
        val c = Calendar.getInstance()

        val iName = name.text.toString()
        val iEmail = email.text.toString()
        val aDay = fillLeft(c.get(Calendar.DAY_OF_MONTH).toString(), "0")
        val aMonth = fillLeft((c.get(Calendar.MONTH) + 1).toString(), "0")
        val aYear = fillLeft(c.get(Calendar.YEAR).toString(), "0")

        val lang = Locale.getDefault().language
        var dateGlobal = ""
        when (lang.toLowerCase()) {
            "en" -> dateGlobal = aYear + "-" + aMonth + "-" + aDay
            "es" -> dateGlobal = aDay + "-" + aMonth + "-" + aYear
            "pt" -> dateGlobal = aDay + "-" + aMonth + "-" + aYear
        }
        val inter = (interpretation as EditText).text.toString()
        val id = Integer.valueOf(ID!!)
        val isOk = !iName.equals("") && !iEmail.equals("") && iEmail.contains("@") && iEmail.contains(".")
        if (isOk)
            SendToInterpret().execute(ID,iName,iEmail,qDate,tTime,keypub,dateGlobal,inter
            )
        else
            Toast.makeText(mContext,getString(R.string.toastNameAndEmailEmpty),Toast.LENGTH_LONG).show()
    }

    val btnclose = window!!.findViewById<View>(R.id.btnCloseInterpret) as TextView
    btnclose.setOnClickListener { setGone() }

    window!!.requestLayout()
    return window
}
  • 1

    Since you are using Kotlin, take a look at the View Binding plugin of Android Extensions, with it you no longer need findViewById, just call the view by its Id in the layout (at the appropriate time, in this case, onViewCreated) https://kotlinlang.org/docs/tutorials/android-plugin.html

1 answer

1

I think the problem is in the variable (possibly global) window. You are assigning her the root layout and then using the findViewById() with her. What’s more, she’s doing all this yet onCreateView, views should be handled only in onViewCreated, called right after onCreateView has returned its view, and only then use it in a "safe state".

My suggestion is, use the root view returned by onCreateView on onViewCreated to instantiate your other views.

class AcvtFragmentView : Fragment() {
    // ...
    internal var actionCallBack: onActionEventListener? = null

    interface onActionEventListener {
        fun dlgActions(id: Int, name: String?, email: String?, datepub: String?, interpretation: String?)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
        try {
            actionCallBack = context as onActionEventListener
        } catch (e: ClassCastException) {
            throw ClassCastException("$activity must implement onSomeEventListener")
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mydb = DreamsItemHelper(mContext)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.layout_interprete, container, false)
    }

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

        val ID = arguments!!.getString("ID")
        val dDate = arguments!!.getString("date")
        val tTime = arguments!!.getString("time")
        val keypub = arguments!!.getString("keypub")
        val interp = arguments!!.getString("msg")

        // date = view.findViewById<View>(R.id.datepubItView) as TextView
        date = view.findViewById<TextView>(R.id.datepubItView)
        date.text = dDate

        val kName = arguments!!.getString("name")
        val kEmail = arguments!!.getString("email")

        //val name = view.findViewById<View>(R.id.interName) as EditText
        val name = view.findViewById<EditText>(R.id.interName)
        name.setText(kName)

        // val email = view.findViewById<View>(R.id.interEmail) as EditText
        val email = view.findViewById<EditText>(R.id.interEmail)
        email.setText(kEmail)

        // interpretation = view.findViewById<View>(R.id.edtInterpretation) as EditText
        interpretation = view.findViewById<EditText>(R.id.edtInterpretation)
        interpretation.text = interp

        //btnsend = view.findViewById<View>(R.id.btnSendToInterpret) as TextView
        btnsend = view.findViewById<TextView>(R.id.btnSendToInterpret)
        if (interp != null) {
            btnsend.isEnabled = false
        }

        btnsend.setOnClickListener {
            val c = Calendar.getInstance()

            val iName = name.text.toString()
            val iEmail = email.text.toString()
            val aDay = fillLeft(c.get(Calendar.DAY_OF_MONTH).toString(), "0")
            val aMonth = fillLeft((c.get(Calendar.MONTH) + 1).toString(), "0")
            val aYear = fillLeft(c.get(Calendar.YEAR).toString(), "0")

            val lang = Locale.getDefault().language
            val dateGlobal = when (lang.toLowerCase(Locale.getDefault())) {
                "en" -> "$aYear-$aMonth-$aDay"
                "es" -> "$aDay-$aMonth-$aYear"
                "pt" -> "$aDay-$aMonth-$aYear"
                else ->  ""
            }

            val inter = (interpretation as EditText).text.toString()
            val id = Integer.valueOf(ID)
            val isOk = iName != "" && iEmail != "" && iEmail.contains("@") && iEmail.contains(".")
            if (isOk)
                SendToInterpret().execute(ID, iName, iEmail, qDate, tTime, keypub, dateGlobal, inter)
            else
                Toast.makeText(mContext, getString(R.string.toastNameAndEmailEmpty), Toast.LENGTH_LONG).show()
        }

        //val btnclose = view.findViewById<View>(R.id.btnCloseInterpret) as TextView
        val btnclose = view.findViewById<TextView>(R.id.btnCloseInterpret)
        btnclose.setOnClickListener { setGone() }

        //view.requestLayout()
    }
}
  • Opa Lennoard, I positive the answer because it is correct, but I resolved otherwise, which was extending my class of a Dialogfragment instead of a Fragment, Valeus, "when you marry, the woman eh yours", rererere

Browser other questions tagged

You are not signed in. Login or sign up in order to post.