https://github.com/google-developers-sohag/navigationandfragments
A use case for navigable fragments.
https://github.com/google-developers-sohag/navigationandfragments
android-application android-studio androidx-fragment fragment-container-view koltin-android kotlin
Last synced: 8 months ago
JSON representation
A use case for navigable fragments.
- Host: GitHub
- URL: https://github.com/google-developers-sohag/navigationandfragments
- Owner: Google-Developers-Sohag
- Created: 2022-04-02T16:46:39.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-04-03T19:52:03.000Z (over 3 years ago)
- Last Synced: 2025-01-08T07:41:47.024Z (10 months ago)
- Topics: android-application, android-studio, androidx-fragment, fragment-container-view, koltin-android, kotlin
- Language: Kotlin
- Homepage: https://developer.android.com/guide/fragments/create
- Size: 104 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# NavigationAndFragments
A use case for fragments and navigation.
## To implement this use case, follow these steps :
1) Create a new fragment navigation xml file with id `main_nav`:
```xml
```
2) Navigate to the activity class xml file and add a new `FragmentContainerView` Ui view tag and add the property `app:navGraph` with the
navigation file id `main_nav`, now the navigation controller will adapt and render its data on the `FragmentContainerView` on runtime :
```xml
```
3) Add 2 fragment placeholders xml tags in the above navigation tag with these respective Ids and setup a navigation action for
each fragment with an id and destination parameters :
```xml
```
4) Add a start destination property for default fragment to be rendered on application start and define the id for the start fragment :
```xml
app:startDestination="@id/blankFragment"
```
5) Create 2 new fragments, file > create new > fragment > fill in your options.
6) Add buttons to control the navigation between fragments :
- Those are our 2 fragments and their respective designs and naviagtion controllers :
```xml
```
```xml
```
7) Add listeners for our buttons (`btn_next` and `btn_previous`) to control our fragment navigation :
- Get the navigation view instance (`main_nav`) from the child fragment view :
```kt
navController = Navigation.findNavController(view)
```
- Add a button listener and fire the navigation actions defined under each fragment :
```kt
binding.btnNext.setOnClickListener {
navController.navigate(R.id.action_blankFragment_to_secondFragment)
}
```
## Classes References :
- A `Fragment` : A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.
- A `FragmentContainerView` : is a customized Layout designed specifically for Fragments. It extends FrameLayout, so it can reliably handle Fragment Transactions, and it also has additional features to coordinate with fragment behavior.
- Notice : FragmentContainerView will only allow views returned by a Fragment's `Fragment.onCreateView`. Attempting to add any other view will result in an IllegalStateException.