010 Navigation and ViewBinding
Overview of Navigation and View Binding in Android
Introduction to Navigation Setup
- The lecture focuses on setting up navigation and view binding, marking the completion of UI components before transitioning to coding.
- A new Android resource file is created for navigation, specifically named
nav_graph, which will serve as the basic graph for fragment management.
Fragment Class Creation
- Fragments are not visible initially because their corresponding classes have not been created; only layouts exist at this point.
- A new package called
viewis established, along with additional packages likemodelandroom_db, indicating a structured approach to organizing code.
- An
ArtFragmentclass is created by extending the Fragment class, utilizing layout resources directly in the constructor instead of overriding methods likeonCreateView.
Implementing View Binding
- The latest feature allows direct use of layout resources in constructors, simplifying fragment setup without needing to override traditional lifecycle methods.
- The instructor proceeds to create a second fragment class named
ArtDetailsFragment, which will handle user input for names and years.
Additional Fragment Classes
- Another class called
ImageApiFragmentis introduced, also extending Fragment and using layout resources appropriately.
- After creating these classes, a project build is initiated to synchronize changes and make fragments visible in the navigation graph.
Configuring Navigation Actions
- The art fragment serves as an entry point; actions are defined between fragments (from ArtFragment to ArtDetailsFragment and then to ImageApiFragment).
- Rebuilding the project generates necessary classes for navigation directions within the Java folder.
Main Activity Configuration
- In MainActivity's XML layout, a TextView is replaced with a NavHostFragment that links to the navigation graph.
- The newer method suggests using a FragmentContainerView instead of traditional fragment tags for better compatibility with modern practices.
Finalizing Navigation Setup
- The starting destination for navigation is confirmed as ArtFragment; another rebuild ensures all connections are intact.
Testing Weave Binding in Art Fragment
Introduction to Weave Binding
- The speaker is testing the functionality of weave binding within an art fragment, indicating a transition from traditional methods.
- Weave binding is highlighted as a more efficient alternative to
findViewById, which iterates through all views to locate a specific ID.
Benefits of Weave Binding
- The implementation of build features enables automatic class generation, enhancing performance and reducing manual coding efforts.
- The generated class for the fragment will be named
FragmentArtBinding, emphasizing the importance of naming conventions in binding creation.
Initializing Fragment Binding
- The speaker demonstrates how to initialize the fragment binding variable, allowing access to root layout views without using
findViewById.
- By utilizing
FragmentArtBinding.bind(view), developers can streamline view access and improve code readability.
Utilizing Fragment Binding
- With the global variable for fragment binding established, it allows for easy interaction with UI components like Floating Action Button (FAB).
- The navigation process is explained using automatically generated classes, specifically navigating from the art fragment to details fragment.
Conclusion on Navigation and Efficiency