The samples in the previous article show how you can create
Scenes with just the basic interfaces, but there is a
lot of boilerplate setup taking place.
Fortunately, there are some base implementations that take some of this
boilerplate out of your hands.
BasicScene1
The BasicScene class is a basic abstract Scene class that
provides most common functionality, such as providing a handle to the currently
attached view, saving view state between subsequent attach
calls, and provides
a default saveInstanceState()
implementation for easy state saving.
It does not implement SavableScene by itself, but subclasses of
BasicScene can opt in to this default implementation by explicitly implementing
the 'SavableScene' interface.
If we take the sample from Scene state restoration before and re-implement it
using the BasicScene class, we get the following:
1 interface MyContainer: RestorableContainer
2
3 class MyScene(
4 private val userId: String,
5 savedState: SceneState? = null
6 ) : BasicScene<MyContainer>(savedState),
7 SavableScene {
8
9 override fun saveInstanceState(): SceneState {
10 return super.saveInstanceState().also {
11 it["user_id"] = userId
12 }
13 }
14
15 companion object {
16
17 fun create(savedState: SceneState) : MyScene {
18 return MyScene(
19 savedState["user_id"],
20 savedState
21 )
22 }
23 }
24 }
We now only have to deal with saving and restoring our userId
, and let the
BaseSavableScene handle the rest.
RxScene2
The RxScene abstract class extends the BasicScene class and provides helper functions for working with Rx streams.
LifecycleScene3
The LifecycleScene abstract class extends the
BasicScene class and implements the
androidx.lifecycle.LifecycleOwner
interface.
1: This class is available in the ext-acorn
artifact.
2: This class is available in the ext-acorn-rx
artifact.
3: This class is available in the ext-acorn-android-lifecycle
artifact.