0

I've been struggling with this for quite some time now, a lot of people suggested that one uses the FragmentManager to rebuild the fragment (such as Show hide fragment in android ,among other methods). Here is my situation:

  1. I have the MainActivity telling the Fragment it should update itself. Fine until here.
  2. The correct Fragment is receiving the instructions, and it tries to toggle the visibility of its Views (TextView, Button,etc), which, in theory, it does.
  3. Problem is, the Fragment's view, isn't updated, that is, the button's and textview's visibility does not change. How come? Oddly enough, for testing purposes, I placed my Fragment in 3 tabs at once, and they are actually updated, that is, when I cycle to them, their visibility is changed, and when I return to my tab, it is changed as well, that is, the visibility is now the intended.

Is this normal? Is there an official way to change this - one that's not "hacky" and involves rebuilding the Fragment?

Even stranger is, the toggle on the Fragment builds a Toast, which isn't shown. However if I simply create the Toast from the MainActivity (where the Fragment is), it works. Should I just toggle the visibility from the MainActivity? I would really like to avoid this.

Community
  • 1
  • 1
  • Your confusion confuses others. lol Fragment views are updated through the Activity context such as from a Fragment: Button b = (Button) getActivity().findViewById(R.id.some_button); That means the Activity has control of the views but since the Fragment can call getActivity() it can also access the views to update them. When calling b.setVisibility(View.GONE) the view in the fragment disappears. You don't need to do anything else to change visibility. Beyond that can't figure out what your doing with your Fragments or views or even saying. The code has to run or it doesn't happen. – CmosBattery Jan 12 '16 at 00:42
  • Actually that is exactly what I was looking for: I didn't know Fragments' view had to be updated by the activity, and not by the fragment itself. Thanks mate – user4218673 Jan 12 '16 at 11:41

0 Answers0