Code Monkey home page Code Monkey logo

fragivity's Introduction

Fragivity : Use Fragment like Activity

English | 中文文档

Bintray Language License

  • More 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

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?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navHostFragment = supportFragmentManager
            .findFragmentById(R.id.nav_host) as NavHostFragment

        navHostFragment.loadRoot(HomeFragment::class)
        
        //or load root with factory
        //navHostFragment.loadRoot{ HomeFragment() }

    }
}

3. navigate to destination Fragment

//in HomeFragment
val bundle = bundleOf(KEY_ARGUMENT1 to "arg1", KEY_ARGUMENT2 to "arg2")
navigator.push(DestinationFragment::class, bundle)

Launch Mode

Support multiple launch modes

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

Transition Animation

navigator.push(LaunchModeFragment::class) {
    enterAnim = R.anim.slide_in
    exitAnim = R.anim.slide_out
    popEnterAnim = R.anim.slide_in_pop
    popExitAnim = R.anim.slide_out_pop
}

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

  //...

  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)

License

Fragivity is licensed under the MIT License.

fragivity's People

Contributors

vitaviva avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.