Scene is a lightweight library of navigation and ui composition based on view.
Download the latest Sample APK
Scene is designed to replace the use of Activity and Fragment on navigation and page segmentation.
The main problems of Activity:
The main problems of Fragment:
The Scene framework tries to solve these problems of the Activity and Fragment mentioned above.
Provides a simple, reliable, and extensible API for a lightweight navigation and page segmentation solution
At the same time, we provide a series of migration solutions to help developers gradually migrate from Activity and Fragment to Scene.
Add it to your root build.gradle at the end of repositories:
//build.gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
//or settings.gradle.kts
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
...
maven { url = uri("https://jitpack.io") }
}
}
Add it to your build.gradle, latest_version :
dependencies {
implementation 'com.github.bytedance:scene:$latest_version'
//or
implementation 'com.github.bytedance.scene:scene:$latest_version'
implementation 'com.github.bytedance.scene:scene_navigation:$latest_version'
implementation 'com.github.bytedance.scene:scene_ui:$latest_version'
implementation 'com.github.bytedance.scene:scene_dialog:$latest_version'
implementation 'com.github.bytedance.scene:scene_shared_element_animation:$latest_version'
implementation 'com.github.bytedance.scene:scene_ktx:$latest_version'
}
//or build.gradle.kts
dependencies {
implementation ("com.github.bytedance:scene:$latest_version")
//or
implementation ("com.github.bytedance.scene:scene:$latest_version")
implementation ("com.github.bytedance.scene:scene_navigation:$latest_version")
implementation ("com.github.bytedance.scene:scene_ui:$latest_version")
implementation ("com.github.bytedance.scene:scene_dialog:$latest_version")
implementation ("com.github.bytedance.scene:scene_shared_element_animation:$latest_version")
implementation ("com.github.bytedance.scene:scene_ktx:$latest_version")
}
For simple usage, just let your Activity inherit from SceneActivity:
class MainActivity : SceneActivity() {
override fun getHomeSceneClass(): Class<out Scene> {
return MainScene::class.java
}
override fun supportRestore(): Boolean {
return false
}
}
A simple Scene example:
class MainScene : AppCompatScene() {
private lateinit var mButton: Button
override fun onCreateContentView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View? {
val frameLayout = FrameLayout(requireSceneContext())
mButton = Button(requireSceneContext())
mButton.text = "Click"
frameLayout.addView(mButton, FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))
return frameLayout
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setTitle("Main")
toolbar?.navigationIcon = null
mButton.setOnClickListener {
navigationScene?.push(SecondScene())
}
}
}
class SecondScene : AppCompatScene() {
private val mId: Int by lazy { View.generateViewId() }
override fun onCreateContentView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View? {
val frameLayout = FrameLayout(requireSceneContext())
frameLayout.id = mId
return frameLayout
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setTitle("Second")
add(mId, ChildScene(), "TAG")
}
}
class ChildScene : Scene() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View {
val view = View(requireSceneContext())
view.setBackgroundColor(Color.GREEN)
return view
}
}
copy FragmentScene to your project
class YourFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
return View(requireContext())
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val parentScene = ViewUtlity.findScene(this.view()) as YourFragmentScene
}
}
class YourFragmentScene : FragmentScene() {
override val fragmentClass = YourFragment::class.java
}
https://github.com/bytedance/scene/wiki/Compose
Scene sample is built using Gradle. On Linux, simply run:
./gradlew installDebug
https://github.com/bytedance/scene/wiki
A normal Dialog's Window is independent and in front of the Activity's Window, so if try to push a Scene in a opening Dialog, it will cause the Scene to appear behind it. You can close the dialog box when click, or use transparent Scene to implement the dialog instead of a system Dialog.
Apps using Scene
Xigua Video | Tik Tok | Toutiao |
Copyright (c) 2019 ByteDance Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.