Skip to content
qiibeta edited this page Dec 7, 2019 · 17 revisions

Scene is designed to replace the use of Activity and Fragment on navigation and page segmentation.

The main problems of Activity and Fragment

The main problems of Activity:

  1. The stack management of Activity is weak, Intent and LaunchMode are confusing, even if various of hacks still can't completely avoid issues like black screen
  2. The performance of Activity is poor, average startup time of an empty Activity is more than 60ms (on Samsung S9)
  3. Because the Activity is forced to support states recovery, it causes some problems:
    • Transition animation has limited ability, difficult to implement complex interactive animations.
    • Shared-element animation is basically unavailable, and there are some crashes unresolved in Android Framework.
    • Every time starting a new Activity, onSaveInstance() of the previous Activity must be executed completely first, which will lose much performance.
  4. Activity relies on the Manifest file to cause injection difficulties, which also result in that Activity dynamics requires a variety of hacks

The main problems of Fragment:

  1. There are many crashes that the Google official can't solve for a long time. Even if you don't use Fragment, it may still trigger a crash in the OnBackPressed() of AppCompatActivity.
  2. The add/remove/hide/show operation is not executed immediately. With nest Fragments even if you use commitNow(), the status update of the sub Fragments cannot be guaranteed.
  3. Fragment has two lifecycles, View lifecycle and Fragment instance lifecycle.
  4. Fragment show/hide method only modify view visibility, none lifecycle method is invoked.
  5. The support of animation is poor, Z-axis order cannot be guaranteed when switching.
  6. The lifecycle of Fragment in native Fragment and Support-v4 packages is not exactly the same.
  7. View.OnClickListener onClick callback getActivity() == null when Fragment is destroyed.

The main problems of Navigation Component:

  1. There is no method can be used to modify Fragment theme.
  2. Navigation management is weak, there is no advanced stack management except for basic open/close singleTop singleTask.
  3. Rely Android Studio to define navigation graph.

How Scene solve above problems

  1. Only have one Lifecycle, Scene' view and Scene destroy together.
  2. NavigationScene responsible for backStack, GroupScene responsible for UI composition.
  3. NavigationScene and GroupScene operation will execute immediately exception for extreme cases (for example, push another Scene in its lifecycle method callback).
  4. Scene framework can disable state restore, then you can use a parameterized constructor Scene.
  5. Animation will be executed after Scene finished lifecycle, animation api can get two Scene's view.
  6. When Scene is destroying, cancel all pending click/longClick action via View.cancelPendingInputEvents()

Components to Know

Scene Components
Scene The Scene is the View wrapper with lifecycle management.
NavigationScene The NavigationScene implements navigation and backStack handling for Scene.
GroupScene The GroupScene can add other Scene/NavigationScene/GroupScene as child.

Scene

NavigationScene

GroupScene

Need attention

Scene created View's Context is a ContextWrapper, not Activity. You can fix this by override onGetLayoutInflater(), then Scene.setTheme will not working.

abstract class ActivityContextGroupScene extends UserVisibleHintGroupScene {
    @Override
    protected final LayoutInflater onGetLayoutInflater() {
        return requireActivity().getLayoutInflater();
    }
}

abstract class ActivityContextScene extends Scene {
    @Override
    protected final LayoutInflater onGetLayoutInflater() {
        return requireActivity().getLayoutInflater();
    }
}