Getting started

This page provides a quick overview on getting started with Acorn. Be sure to have the necessary dependencies included as shown in Setup.

Acorn provides several helper classes that can get you started quicky. The easiest way is to have your main Activity extend from AcornActivity or AcornAppCompatActivity and implement the provideNavigatorProvider method:

object MyNavigatorProvider : NavigatorProvider { /* ... */ }

class MainActivity : AcornActivity() {

    override fun provideNavigatorProvider() : NavigatorProvider {
        return MyNavigatorProvider
    }
}

If you can't or don't want to extend from AcornActivity, you can use the AcornActivityDelegate class.

Scene/Container/ViewController

You can create a very simple Scene to get started by implementing the BaseSavableScene class and implementing the ProvidesView interface:

 1 interface MyContainer : Container
 2 
 3 class MyViewController(override val view: View) : RestorableViewController, MyContainer
 4 
 5 class MyScene : BaseSavableScene<MyContainer>(null), ProvidesView {
 6 
 7     override fun createViewController(parent: ViewGroup) : ViewController {
 8         val layout = LayoutInflater.from(parent.context)
 9             .inflate(R.layout.my_scene, parent, false) 
10             
11         return MyViewController(layout)
12     }
13 }

Passing null to the BaseSavableScene constructor basically implies the Scene does not support state restoration.
See Scenes for more information on how to create Scenes.

The simplest of Navigators is one that extends the SingleSceneNavigator class:

class MyNavigator : SingleSceneNavigator(null) {

    override fun createScene(state: SceneState?) : Scene<out Container> {
        return MyScene()
    }
}

Passing null to the SingleSceneNavigator constructor basically implies the Navigator does not support state restoration.
See Navigators for more information on how to create Navigators.

You will need to implement the NavigatorProvider interface to let Acorn know which Navigator to use.
The AbstractNavigatorProvider class provides a base implementation you can use:

object MyNavigatorProvider : AbstractNavigatorProvider<MyNavigator>() {

    override fun createNavigator(savedState: NavigatorState?) : MyNavigator {
        return MyNavigator()
    }
}

The NavigatorProvider needs to be cached between Activity instances.

See Navigators for more information.

Configuration

The setup in the previous section falls back to the default configuration Acorn provides. There are a couple of things you can customize.

ViewControllerFactory

By default, Acorn relies on Scenes implementing the ViewControllerFactory interface for creating ViewController instances. You can also supply your own ViewControllerFactory instance to Acorn by overriding the provideViewControllerFactory function in AcornActivity, or passing it to the AcornActivityDelegate.

See ViewControllerFactories for more information on creating ViewControllerFactories.

TransitionFactory

Acorn will use default transition animations to animate transition between Scenes. If you want to provide custom transition animations, you need to implement the TransitionFactory interface and supply it to Acorn by overriding the provideTransitionFactory function in AcornActivity, or passing it to the AcornActivityDelegate.

See Transition Animations for more information on creating TransitionFactories.

ActivityControllerFactory

If your application needs to start external Activities you need to implement the ActivityControllerFactory interface and supply it to Acorn. You can do this by overriding the provideActivityControllerFactory function in AcornActivity, or by passing it to the AcornActivityDelegate.