Search code examples
androidkotlinandroid-dialogfragment

I can't change the text of textView from a DialogFragment


I'm facing this problem and don't have any idea how to solve it. It works inside the dialog class, but not in the activity class.

I tried all i found about this problem: creating the setter method, invoking the method after showing the dialog, the id is correct so idk. This is the dialog class:

class CustomDialogFragment: DialogFragment() {
    private lateinit var textView: TextView
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        isCancelable = false
        val view = inflater.inflate(R.layout.winner_dialog, container, false)
        textView = view.findViewById(R.id.dialogtextView)
        return view
    }
    fun setTextViewText(winner: String) {
        activity?.runOnUiThread {
            textView.text = winner
        }
    }
}

Of course I created an instance of this class and used it like this:

if(Check.checkForX(boxes)){
      dialog.show(supportFragmentManager, "customDialog")
      if (firstPlayer != null) {
           dialog.setTextViewText(firstPlayer)
      }
}

P.S: the dialog box pops up, just the text in it is not changing. Thank you!


Solution

  • I think it's probably because you're trying to call this function immediately after dialog.show:

    public void show(@NonNull FragmentManager manager, @Nullable String tag)

    Display the dialog, adding the fragment to the given FragmentManager. This is a convenience for explicitly creating a transaction, adding the fragment to it with the given tag, and committing it.

    And as the commit documentation explains:

    public abstract int commit()

    Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.

    So basically when you call show, you're requesting the dialog be shown - but it's not added yet! So it's not attached to its Activity, and this

    activity?.runOnUiThread
    

    won't run, because activity is null at this point.


    The other issue is that onCreate is called after onAttach (i.e. after the activity is accessible) which means onCreateView hasn't run yet either, and you don't have a reference to textView to set text on. You basically can't call this function at all yet, even if you remove the activity reference.

    You could update the function to store the current text value if you don't have textView yet, and set that text during onCreateView if you have it stored (deferring the task basically). This is useful if you're gonna need to update that text at different times, but if this is a one-shot setup thing, it would be easier to just set the text as a Fragment argument, and pull it out during onCreateView or whatever, like in the example here (they're doing it in a newInstance method so the details are internal to the dialog's class - you just pass a string in as a parameter when you actually want an dialog object).