0

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).

enter image description here

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.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Peter Weyand
  • 2,159
  • 9
  • 40
  • 72
  • So, you want to change text whenever you change the tabs? – willy wijaya Feb 21 '18 at 03:40
  • Yes! I've got it figured out how to (finally) send data through a newInstance via TabsAdapter. But after I got all that set up I find I can't even update the value of the textView at all. I have no idea why this should be this hard. – Peter Weyand Feb 21 '18 at 03:42

1 Answers1

1

Change your method to return a view instead of inflating it again as

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 view //< --- do this
    }
}
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37