Events

Often, certain events happen in a screen that should cause a transition to another screen. This could be a user having logged in successfully, or pressing on an item to view its detail screen. A Scene however should have no knowledge of navigation flow, meaning it cannot tell the system to go to another Scene. Instead, it can notify the component that does have control over navigation flow that an event has happened, like "this item was clicked!".

A typical pattern for this is to define an interface for these events, and let the Scene accept an instance of this interface in its constructor. The listener can then act on these events accordingly, such as navigating to another Scene.

 1 class LoginScene(
 2     private val loginInteractor: LoginInteractor,
 3     private val listener: Events
 4 ) : Scene<LoginContainer> {
 5 
 6     /* ... */
 7 
 8     private fun doLogin(username: String, password: String) {
 9         loginInteractor.login(username, password) { user : User ->
10             listener.onLoggedIn(user)
11         }
12     }
13 
14     interface Events {
15 
16         /** Called when this Scene is done logging in given user. */
17         fun onLoggedIn(user: User)
18     }
19 }

This technique decouples the actual act of navigating to another screen from the Scene implementation, allowing for a modular and reusable component:

For more information about navigation, see Navigators.