English | 中文文档
Fragivity is a library used to build APP with "Single Activity + Multi-Fragments" Architecture
- Reasonable Lifecycle: Lifecycle is consistent with Activity when screen changed
- Multiple LaunchModes: Supports multiple modes, such as Standard, SingleTop and SingleTask
- Transition animation: Supports Transition or SharedElement animation when switching screens
- Efficient communication: Simple and direct communication based on callback
- Friendly Backpress: Supports onBackPressed interception and SwipeBack
- Deep Links: Routes to the specified screen by URI
- Dialog: Supports DialogFragment
JCenter is shutting down and the lib after(including) 0.2.1 can be get from JitPack.
!Note: group name changed from
com.github.fragivity
tocom.github.vitaviva.fragivity
implementation 'com.github.fragivity:core:$latest_version'
// add in your root build.gradle at the end of repositories
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
implementation 'com.github.vitaviva.fragivity:core:$latest_version'
Like Navigation
, Fragivity needs a NavHostFragment
as the host of ChildFragments
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true" />
</FrameLayout>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host) as NavHostFragment
navHostFragment.loadRoot(HomeFragment::class)
//or loadRoot with factory
//navHostFragment.loadRoot{ HomeFragment() }
}
}
//in HomeFragment
navigator.push(DestinationFragment::class) {
arguments = bundleOf(KEY_ARGUMENT1 to "arg1", KEY_ARGUMENT2 to "arg2")
//or applyArguments(KEY_ARGUMENT1 to "arg1", KEY_ARGUMENT2 to "arg2")
}
Support multiple launch modes
navigator.push(DestinationFragment::class) {
launchMode = LaunchMode.STANDARD //default
//or LaunchMode.SINGLE_TOP, LaunchMode.SINGLE_TASK
}
navigator.push(DestinationFragment::class) {
//animator
enterAnim = R.anim.slide_in
exitAnim = R.anim.slide_out
popEnterAnim = R.anim.slide_in_pop
popExitAnim = R.anim.slide_out_pop
//sharedElements
sharedElements = sharedElementsOf(imageView to "id")
}
You can simply setup communication between two fragments
class HomeFragment : Fragment(){
private val cb: (Int) -> Unit = { checked ->
//...
}
//...
fun startDestination() {
navigator.push {
DestinationFragment(cb)
}
}
//...
}
class DestinationFragment(val cb: (Int) -> Unit) : Fragment() {
//...
cb.invoke(xxx)
//...
}
class DialogFragment : DialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_dialog, container, false)
return root
}
}
navigator.showDialog(DialogFragment::class)
kapt 'com.github.fragivity:processor:$latest_version'
@DeepLink(uri = "myapp://fragitiy.github.com/")
class DeepLinkFragment : Fragment() {
//...
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//...
navHostFragment.handleDeepLink(intent)
}
}
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("myapp://fragitiy.github.com/"))
startActivity(intent)
with(navHostFragment) {
composable("feed") { FeedFragment.newInstance() }
composable("search?keyword={keyword}", stringArgument("keyword")) {
SearchFragment.newInstance()
}
}
navigator.push("search?keyword=$value")
// or
navigator.push("search") {
arguments = bundleOf("keyword" to value.toString())
}
navigator.popTo("search")
Fragivity provides a set of APIs for Java developers
Fragivity is licensed under the MIT License.