Navigation

RoutePath

A class that defines a path to the screen. The properties of this class are parameters that you want to send to the screen. Use a simple class when the path doesn't provide any parameters to the screen. Use a data class when you want to pass one or more parameters. Avoid passing heavy data like images or long arrays through it. It is usually used to pass small portions of data such as IDs or filter parameters.

Examples:

class MainPath: RoutePath //Simple path

data class StepPath(val step: Int): RoutePath //Path with parameters

data class DialogPath(val title: String = "",
                      val message: String = "",
                      val okBtn: String = "",
                      val cancelBtn: String = ""): RoutePathResult<Boolean>  //Path with parameters that returns Boolean value

RouteController

A class that connects RoutePath, Screen, and serves to implement custom routing logic. This class is marked with the @Route annotation and implements one of the children of the RouteControllerInterface. Currently, we have four predefined RouteController classes:

  1. Simple with only Fragment or Compose as view RouteController

  2. With only Fragment or Compose as View and Dagger injection RouteControllerC. The letter C means Component that passes in the Controller for injection

  3. Fragment or Compose as View and ViewModel RouteControllerVM. VM means ViewModel

  4. Fragment or Compose as View and ViewModel and Dagger injection RouteControllerVMC

By default this class should be abstract because all its method generated by the framework. You can override its if you need to do some extra action, for example add something to the Bundle of the Fragment.

Examples:

class SimplePath: RoutePath

@Route
abstract class SimpleRouteController: RouteController<SimplePath, SimpleScreen>()

// Then somewhere in code
router.route(SimplePath())
data class SimplePath(val title: String): RoutePath

@Route
class SimpleRouteController: RouteController<SimplePath, SimpleScreen>()
{
    override fun onCreateView(path: SimplePath): SimpleScreen = // logic of creation of the appropriate Screen
}

// Then somewhere in code
router.route(SimplePath("I'm a title"))

Simple scheme

Example for Fragment

Example with no parameters to transfer

class SimplePath: RoutePath

@Route
abstract class SimpleRouteController: RouteController<SimplePath, SimpleFragment>()

// Then somewhere in a Fragment or ViewModel
router.route(SimplePath())

With parameters

data class SimplePath(val title: String): RoutePath

@Route
class SimpleRouteController: RouteController<SimplePath, SimpleFragment>()
{
    override fun onCreateView(path: SimplePath): SimpleFragment =
        SimpleFragment().apply {
            arguments = Bundle().apply { // Put some data to Bundle
                putString("TITLE_KEY", path.title)
            }
        }
}

// Then somewhere in a Fragment or ViewModel
router.route(SimplePath("I'm a title"))

Full explanation and examples you can find here

Example for Compose

Example with no parameters to transfer

class SimplePath: RoutePath

@Route
abstract class SimpleRouteController: RouteController<SimplePath, SimpleView>()

// Then somewere in a Compose view
LocalRouter.currentOrThrow.route(SimplePath())

// Or in a ViewModel
router.route(SimplePath())

With parameters

data class SimplePath(val title: String): RoutePath

@Route
class SimpleRouteController: RouteController<SimplePath, SimpleView>()
{
    override fun onCreateView(path: SimplePath): SimpleView = SimpleView(path.title)
}

// Then somewere in a Compose view
LocalRouter.currentOrThrow.route(SimplePath("I'm a title"))

// Or in a ViewModel
router.route(SimplePath("I'm a title"))

Full explanation and examples you can find here

Last updated