Home > Net >  Android viewBinding usage
Android viewBinding usage

Time:07-14

I have a question about viewBinding in Android. Usually, I use it like this

lateinit var binding: ActivityMainBinding

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

But I also saw that someone uses it like this

private val binding: ActivityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) 

setContentView(binding.root)

Is there any difference? What is the better way?

CodePudding user response:

Sometimes we encounter a scenario where we want to avoid initializations of some fields in a class till they are actually needed. A classic example could be, the case of heavy singletons. That is when you use lazy.

Normally, properties declared as having a non-null type must be initialized in the constructor. However, fairly often this is not convenient. For example, properties can be initialized through dependency injection, or in the setup method of a unit test. In this case, you cannot supply a non-null initializer in the constructor, but you still want to avoid null checks when referencing the property inside the body of a class.

To handle this case, you can mark the property with the lateinit modifier

The upper scenario is the one which is used because the binding is initialized in a lifecycle method later.

In case of lazy it would be called when it is need in the code ie: setContentView which is again inside a lifecycle method ..

Use any one and stick to it

CodePudding user response:

  1. Fragments

I would not recommend using any of these if you are in a Fragment, as you are welcoming the memory leaks this way. Inside of the onDestroyView() (if Fragment) you have to nullify your ViewBinding instance by doing

override fun onDestroyView() {
    super.onDestroyView()
    binding = null
}

If you are using lazy or lateinit it won't allow you to do that, as lateinit forces you to have non-nullable values, and lazy forces you to have immutable variable.

The best approach is to use it as described in documentation

TLDR;

private var _binding: DialogStartcodeBinding? = null
private val binding get() = requireNotNull(_binding)

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    _binding = MyViewBinding.inflate(inflater, container, false)
    return binding.root
}

 override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}
  1. Activity

Inside of the Activity, I would go either as with Fragment and nullify ViewBinding inside of the onDestroy() or with any of the version you described. But FYI in the documentation the lateinit approach is being used.

TLDR; Use whichever option makes you happier :) Both are fine in the Activity.

CodePudding user response:

Hi there are differences between lateinit and lazy

lateinit -> Only var can be used with this and only Non Nullable values allowed

Lazy -> the object will be created only when it is called, otherwise there will be no object creation. Only val is allowed in lazy

  • Related