An open API service indexing awesome lists of open source software.

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.

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.