I followed a tutorial on getting started with tabhost and fragments, there is the link: https://medium.com/@eijaz/getting-started-with-tablayout-in-android-kotlin-bb7e21783761
But I got a problem, I want to send a value activityID to a fragment that my tabhost contains and I don't know how to do it.
I found on the internet that there is a method setArgument but no one of my objects contains this method.
Did I do something wrong ? Or could someone explain to me how to send my value from my ActivityTabHost to my fragment?
This is my ActivityTabHost.kt
import android.os.Bundle
import training.gutai.apps.gutai.R
import kotlinx.android.synthetic.main.activity_tabhost.*
import android.support.v7.app.AppCompatActivity
import android.util.Log
import training.gutai.apps.gutai.models.TabHostAdapter
class ActivityTabHost : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tabhost)
val activityID = intent.extras.getInt("id")
Log.d("Id", activityID.toString())
val fragmentAdapter = TabHostAdapter(supportFragmentManager, activityID)
viewpager_main.adapter = fragmentAdapter
tabs_main.setupWithViewPager(viewpager_main)
}
}
And this is my TabHostAdapter.kt :
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import training.gutai.apps.gutai.viewControllers.FirstFragment
import training.gutai.apps.gutai.viewControllers.SecondFragment
class TabHostAdapter(fm: FragmentManager, activityID : Int) : FragmentPagerAdapter(fm) {
val fm : FragmentManager = fm
override fun getItem(position: Int): Fragment {
return when (position) {
0 -> {
FirstFragment()
}
else -> {
SecondFragment()
}
}
}
override fun getCount(): Int {
return 2
}
override fun getPageTitle(position: Int): CharSequence {
return when (position) {
0 -> "First Tab"
else -> "Second Tab"
}
}
}
The FragmentPagerAdapter is not having the
setArgumentsmethod. The adapter is only holding the fragments. The fragments are having thesetArgumentsmethod.In your
TabHostAdapteryou can set the valueactivityIDto the desired fragment (FirstFragmentorSecondFragment) using thesetArgumentsmethod. Something like the following, in yourgetItemmethod, before returning the fragment: