• Stars
    star
    298
  • Rank 139,663 (Top 3 %)
  • Language
    Kotlin
  • License
    MIT License
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Use Fragment like Activity

Fragivity : Use Fragment like Activity

English | 中文文档

JitPack Language
License

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

Installation

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 to com.github.vitaviva.fragivity

after 0.2.1

// 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'

before 0.2.0

implementation 'com.github.fragivity:core:$latest_version'

Quick start

1. declare NavHostFragment in layout

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>

2. load HomeFragment in Activity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        proxyFragmentFactory()
        // with java
        // Fragivity.proxyFragmentFactory(this)

        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() }

    }
}

!Note: Add proxyFragmentFactory() to ensure that the fragments can run to onStart/onStop,before super.onCreate()

3. navigate to destination Fragment

//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")
}

Launch Mode

Support multiple launch modes

navigator.push(DestinationFragment::class) {
    launchMode = LaunchMode.STANDARD //default
    //or LaunchMode.SINGLE_TOP, LaunchMode.SINGLE_TASK
}

Transition Animation

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")
}

Communication

You can simply setup communication between two fragments

1. start destination Fragment with a callback

class HomeFragment : Fragment() {
    private val cb: (Int) -> Unit = { checked ->
        //...
    }

    //...

    fun startDestination() {
        navigator.push {
            DestinationFragment(cb)
        }
    }
  
    //...
}

2. callback to source Fragment

class DestinationFragment(val cb: (Int) -> Unit) : Fragment() {
    //...
    cb.invoke(xxx)
    //...
}

Show Dialog

1. declare a DialogFragment

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
    }
}

2. show it

navigator.showDialog(DialogFragment::class)

Deep links

1. add kapt dependencies

kapt 'com.github.fragivity:processor:$latest_version'

2. declare URI with @Deeplink annotation

@DeepLink(uri = "myapp://fragitiy.github.com/")
class DeepLinkFragment : Fragment() {
    //...
}

3. handle intent in MainActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        //...
        
        navHostFragment.handleDeepLink(intent)

    }
}

4. start Activity with URI

val intent = Intent(Intent.ACTION_VIEW, Uri.parse("myapp://fragitiy.github.com/"))
startActivity(intent)

Router

1.composable Fragment in Activity

with(navHostFragment) {
    composable("feed") { FeedFragment.newInstance() }
    composable("search?keyword={keyword}", stringArgument("keyword")) {
        SearchFragment.newInstance()
    }
}

2.navigate to destination Fragment

navigator.push("search?keyword=$value")
// or
navigator.push("search") {
    arguments = bundleOf("keyword" to value.toString())
}

navigator.popTo("search")

Using in Java

Fragivity provides a set of APIs for Java developers

FAQ

Frequently Asked Question

License

Fragivity is licensed under the MIT License.

More Repositories

1

compose-tetris

🧱 A tetris game fully built using Jetpack Compose
Kotlin
661
star
2

ugame

Android自定义View高仿抖音潜艇大挑战小游戏
Kotlin
147
star
3

compose-weather

CuteWeather is a beautiful weather app build in Jetpack Compose
Kotlin
100
star
4

CountdownTimer

Countdown timer app built in Jetpack Compose
Kotlin
42
star
5

cmp-gobang

Compose Multiplatform GoBang
Kotlin
34
star
6

JetpackComposePlayground

compose playground
Kotlin
30
star
7

compose-huarongdao

Compose版华容道
Kotlin
26
star
8

ComposeWaveLoading

Compose 波浪效果进度加载
Kotlin
17
star
9

android-dev-challenge-compose

A puppy adoption app built in Jetpack Compose, which took part in the #AndroidDevChallenge for Compose
Kotlin
14
star
10

AnimatedLike

Kotlin
11
star
11

Bloom

Android Dev Challenge: Week 3 - Speed round
Kotlin
6
star
12

MultipleTheme

支持Android动态切换夜间模式等多主题
Java
6
star
13

DaggerHiltSample

A Sample For Dagger Hilt
Kotlin
5
star
14

flutter_platform_sample

Kotlin
4
star
15

wanandroid-compose

A WanAndroid client based on Jetpack Compose
Kotlin
4
star
16

Pinyin

通过Unicode获取汉字的拼音,并可以处理多音字;提供按拼音排序的简单工具类
Java
3
star
17

CommonGallery

仿微信的图片选择器,支持图片的放大/缩小、旋转、拖拽transition动画打开等
Java
2
star
18

NestedBounceView

通过NestedBounceView可以为RecyclerView增加下拉回弹效果,并且支持NestedScroll联动。这只是增加一个回弹效果的小控件,没有下拉刷新的相关功能
Java
2
star
19

blog

1
star
20

QRCodeScanner

二维码条形码扫描工具,可手动调整扫描框大小
Java
1
star
21

MotionPager

MotionLayout实现ViewPager效果
Kotlin
1
star
22

ContactsView

可从系统电话本中查找、选取联系人的工具
Java
1
star