implementation "com.github.AlexExiv.Router-Android:router:$version"implementation "com.github.AlexExiv.Router-Android:annotations:$version"implementation "com.github.AlexExiv.Router-Android:fragment:$version"// add support of fragmentskapt "com.github.AlexExiv.Router-Android:processor:$version"
In the first step, we need to configure our `Application` class.
Your `Application` class should be inherited from the `FragmentApplication` class and create and initialize the `RouterComponentImpl` in the `onCreateRouter` method.
If you're not using Component injection, the App class should look like this:
The second step involves implementing a simple MainActivity class. This class should inherit from the FragmentActivity class from the bootstrap package.
Using with class we will navigate to the SimpleFragment screen
classSimplePath: RoutePath
Now we have to connect SimplePath with SimpleFragment. In the case of Fragment we have the same way of implementing of RouteControllers and Paths. Look at simple example bellow
It generates the onCreateView method by default if you don't need to pass there extra parameters
Here, we have another example of a simple RouteController. In this case, we aim to pass parameters to the screen. When you need to transmit data to your Fragment view, you'll need to implement onCreateView yourself and deliver the data to the view using the arguments property.
Important: Ensure that the data is serializable to be preserved in the state.
Now let's take a look at how RouteController looks like when our Fragment has a ViewModel
RouteController
When working with ViewModels, it's beneficial to create a typealias of RouteController
typealiasRouteControllerApp<Path, VM, V> = RouteControllerVM<Path, VM, FragmentViewModelProvider, V>// if you don't use Component fo injectiontypealiasRouteControllerApp<Path, VM, V> = RouteControllerVMC<Path, VM, FragmentViewModelProvider, V, AppComponent>// otherwise
If you're not passing arguments to your screen, the definition of RouteController remains almost the same, except for adding SimpleViewModel to the RouteController.
classSimplePath: RoutePath// The same// Simple case@RouteabstractclassSimpleRouteController: RouteControllerApp<SimplePath, SimpleViewModel, SimpleFragment>()
If you wish to pass arguments, you must override the onCreateViewModel method and create the ViewModel as shown in the code below:
classSimplePath(val step: Int): RoutePath// The same// When you need to pass data to the ViewModel you have to override the onCreateViewModel method@RouteabstractclassSimpleRouteController: RouteControllerApp<SimplePath, SimpleViewModel, SimpleFragment>(){overridefunonCreateViewModel(modelProvider: FragmentViewModelProvider, path: SimplePath): SimpleViewModel= modelProvider.getViewModel { SimpleViewModel(path.step, it) }}
SimpleFragment
In this case, we need to inherit our SimpleFragment from the FragmentViewModel generic class and pass our ViewModel as a parameter.
The SimpleFragment class becomes:
classSimpleFragment: FragmentViewModel<SimpleViewModel>(R.layout.fragment_simple){overridefunonViewCreated(view: android.view.View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState) viewModel // will be injected by framework }}
SimpleViewModel
The SimpleViewModel class should inherit from the AndroidViewModel class provided by the bootstrap package.