Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
View Binding is an extremely useful feature introduced by Android Jetpack to easily and safely interact with views. When it comes to using View Binding with Fragments, there are certain patterns to follow to ensure memory safety.
Here's a guide on how to use View Binding with Fragments:
If you haven't already set up View Binding for your project, add the following to your module's build.gradle
:
android { ... viewBinding { enabled = true } }
Then rebuild your project.
Suppose you have a Fragment layout named fragment_example.xml
. The generated binding class will be FragmentExampleBinding
.
Here's how you should use it:
import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import com.example.myapp.databinding.FragmentExampleBinding class ExampleFragment : Fragment() { // Declare the binding instance private var _binding: FragmentExampleBinding? = null // This property is only valid between onCreateView and onDestroyView. private val binding get() = _binding!! override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout with View Binding _binding = FragmentExampleBinding.inflate(inflater, container, false) return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // Use the binding instance to access the views binding.textView.text = "Hello from the Example Fragment!" } override fun onDestroyView() { super.onDestroyView() // Set the binding instance to null when the view is destroyed. // This is important to avoid memory leaks. _binding = null } }
We use a nullable _binding
property to hold our fragment's binding instance. This is important because the binding instance has a reference to the fragment's view, and if we don't clear it, this can cause a memory leak.
onDestroyView()
is the appropriate place to clear our binding instance as the view lifecycle of a fragment can be shorter than the fragment's overall lifecycle (i.e., a fragment's view can be destroyed while the fragment is still alive).
By following this pattern, you can safely use View Binding with Fragments in Android Jetpack.
Enabling View Binding for Fragment in Android:
build.gradle
file:android { ... viewBinding { enabled = true } }
Accessing views using View Binding in Fragment:
// In your Fragment private var binding: FragmentExampleBinding? = null override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { binding = FragmentExampleBinding.inflate(inflater, container, false) return binding!!.root }
View Binding with ViewPager and Fragments in Android:
<!-- fragment_view_pager_example.xml --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ... > <!-- Your UI components here using View Binding --> </RelativeLayout>
Working with included layouts and View Binding in Fragment:
<!-- parent_layout_fragment.xml --> <include layout="@layout/included_layout" />