Answer the question
In order to leave comments, you need to log in
Can't transfer text from one fragment to the textview of another?
Cheto I got confused, this.tv_top must not be null always comes.
It seems that I used to transfer values, but now I can’t remember.
The screen is created and displayed, but in the override fun passData method, if you check, it says that the fragment has not even been added to the screen, and the view does not exist.
class BotFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_bot, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
this.setTargetFragment(TopFragment(),1)
val tf = targetFragment as ITop
button_bot.setOnClickListener {
tf.passData("@@@@@@@@@@")
}
}
}
class TopFragment : Fragment(),ITop {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_top, container, false)
}
override fun passData(data: String) {
this.tv_top.text = data
}
}
Answer the question
In order to leave comments, you need to log in
this.setTargetFragment(TopFragment(),1)
val tf = targetFragment as ITop
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question