Home > database >  Using Binding for accessing UI element of parent activity from fragment
Using Binding for accessing UI element of parent activity from fragment

Time:11-24

So, I am trying to migrate from kotlin synthetic to Jetpack view binding.

Here is the kotlin synthetic code (works fine) that simply set visibility to invisible of TextView in the parent activity from fragment.

import kotlinx.android.synthetic.main.activity_main.*

class FirstFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_first, container, false)

        requireActivity().textView.visibility = View.INVISIBLE
        return view
    }
}

And here is what I'm doing to migrate:

import com.mypc.myapp.databinding.FragmentFirstBinding
import com.mypc.myapp.databinding.ActivityMainBinding
    
class FirstFragment : Fragment() {
    
    private var _binding: FragmentFirstBinding? = null
    private val binding get() = _binding!!
    
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View {
            _binding = FragmentFirstBinding.inflate(inflater, container, false)
            return binding.root
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    
            binding.textView.visibility = View.INVISIBLE
            binding.textview.setOnClickListener {
            Navigation.findNavController(view).navigate(R.id.goto_secondfragment)
        }
        }
    
        override fun onDestroyView() {
            super.onDestroyView()
            _binding = null
        }
    }

I'm getting error as 'Unsolved reference' at 'textview':

binding.textView.visibility = View.INVISIBLE

And at:

binding.textview.setOnClickListener {
            Navigation.findNavController(view).navigate(R.id.goto_secondfragment)
        }

Obviously the compiler is not able to find TextView that is in Activity I've added this line:

import com.mypc.myapp.databinding.ActivityMainBinding

CodePudding user response:

Since your binding is private to MainActivity you can refer to your textView from the MainActivity only. To show/hide this view from FirstFragment you can create a public function in MainActivity and call it from your FirstFragment.

class MainActivity: AppCompatActivity {

    private var _binding: ActivityMainBinding? = null
    private val binding get() = _binding!!

    fun showHideTextView(visible: Boolean) {
        binding.textView.isVisible = visible
    }
}

And in your fragment, you can call:

(requireActivity() as MainActivity).showHideTextView(false) // This will hide the textView

CodePudding user response:

First of all, you should define instance of activity view binding in baseActivity which is a parent class of your MainActivity, and then define method to change your text view like 'showTextView' , after that in the base fragment class initalize base activity instance with casting context object in onAttach method. I provide you some code:

abstract class BaseRegisterActivity : BaseActivity() {

    //---

    protected lateinit var binding: ActivityRegisterBinding


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_register)

        //---
    }

    fun showTextView() {
        binding.textView.visibility = View.VISIBLE
    }
}
abstract class BaseLoginFragment : Fragment(), Injectable {

    //--

    lateinit var baseActivity: BaseRegisterActivity

   
    override fun onAttach(context: Context) {
        super.onAttach(context)
        baseActivity = context as BaseRegisterActivity
    }
    
    
    //--
}
  • Related