I have a super simple Fragment
that exists in a TabAdapter
. I can't for some reason update one of the textViews
. Here is what it looks like. I want to update the textView
that says "hello there home fragment" (the bigger one).
Here is my main activity:
class MainActivity: AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
getSupportActionBar()?.hide();
view_pager.adapter = TabsAdapter(supportFragmentManager)
tab_layout.setupWithViewPager(view_pager)
}
}
Here is my TabsAdapter:
class TabsAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {
private val fragmentTitles = arrayOf("Home", "Music", "Map")
override fun getItem(position: Int): Fragment {
when (position) {
0 ->
return HomeFragment()
1 ->
return MusicFragment()
2 ->
return MapFragment()
else ->
return HomeFragment()
}
}
// Return the number of views/fragments available.
override fun getCount(): Int = 3 // in our case 3 fragments
// Return title based on position
override fun getPageTitle(position: Int): CharSequence {
return fragmentTitles[position]
}
}
And here is my HomeFragment():
class HomeFragment : Fragment() {
var weathermainView: TextView?=null
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.fragment_home, container, false)
val weathermainView = view.findViewById<TextView>(R.id.textweathermain)
println("***********************************************")
println("value of weathermainView in onCreateView BEFORE setting: " + weathermainView?.text)
println("***********************************************")
weathermainView.setText("YOLO DAWG")
println("***********************************************")
println("value of weathermainView in onCreateView AFTER setting: " + weathermainView?.text)
println("***********************************************")
return inflater!!.inflate(R.layout.fragment_home, container, false)
}
}
So the tabs definitely cycle as they should, but when I boot the emulator, and this is the output:
I/System.out: ***********************************************
I/System.out: value of weathermainView in onCreateView BEFORE setting: hello there home fragment
I/System.out: ***********************************************
I/System.out: ***********************************************
I/System.out: value of weathermainView in onCreateView AFTER setting: YOLO DAWG
I/System.out: ***********************************************
HOWEVER
The home screen does not change, even though it says that the textView.text value should be different. I have double checked that the textView item is the correct one in my layouts package. I've also tried setting the text in the onViewCreated lifecycle hook, but that doesn't seem to work either.
This really shouldn't be this hard. I've looked for days on stackoverflow and in the docs, and as best I can tell this is the way you are supposed to do it. Any help would be appreciated.