I am developing an application on android and I encounter a problem.
The existing topics on StackoverFlow do not answer my problem, they are not under kotlin and do not go through 3 class
I would like to display data from an object (Activity). The problem is that it's object is in my MainActivity and I have to pass it through several kotlin classes (MainActivity -> AdapterToto -> UnityInfoFragment.
MainActivity.kt :
val pager = findViewById<ViewPager2>(R.id.viewPager2)
val tab1 = findViewById<TabLayout>(R.id.tabLayoutDetail)
pager.adapter = AdapterToto(supportFragmentManager,lifecycle, Activity())
TabLayoutMediator(tab1, pager){ tab, positon ->
tab.text = tabTitle[positon]
}.attach()
AdapterToto.kt :
class AdapterToto(fragmentManager: FragmentManager,lifecycle: Lifecycle, private val myData : Activity) : FragmentStateAdapter(fragmentManager,lifecycle){
override fun getItemCount(): Int {
return 4
}
override fun createFragment(position: Int): Fragment {
when(position){
0 -> return ActivityInfoFragment()
1 -> {
return UnityInfoFragment(myData)
}
2 -> return StatistiqueFragment()
3 -> return StatistiqueFragment()
else -> return ActivityInfoFragment()
}
}
}
UnityInfoFragment :
class UnityInfoFragment(private val myData: Activity) : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
Log.e("TOTO",myData.toString())
return inflater.inflate(R.layout.fragment_unity_info, container, false)
}
}
Once the data arrives in the fragment, how can I retrieve the textfield and display it dynamically?
Thanks