GithubHelp home page GithubHelp logo

lifecyclin's Introduction

"Lifecyclin"

A Extension of Android Lifecycle Management for Kotlin

init {
       onStart {
           service.start()
       }
       onResume {
           service.refresh()
       }
 }

Get Started

add maven url in to your build.gradle (Normally in root project)

repositories {
    maven {
        url  "http://harrytylenol97.bintray.com/maven"
    }
}

And add library into module dependencies

<!> You should use support lifecycle library beacuse it needed
compile 'com.tylenol.library:lifecyclin:0.2.1'


Features

Add User Defined LifecycleObserver to LifecycleRegistry

First you should define class which inherits LifecycleObserver

class SampleActivity : LifecycleCompatActivity() {
    var myListener = MyListener()
    // ...
}

// ...
class MyListener : LifecycleObserver { /**/ }
addObserver(myListener) // Method 1
val myListener = MyListener().registerObserver(this) // Method 2

You can also add 2 more Observers as vararg

addObserver(myListener).addObserver(myLocationListener).addObserver(myGoogleApiClientListener)
addObserver(myListener, myLocationListener, myGoogleApiClientListener)

Define Units(functions) to each lifecycle components at once (Automatically Add it as Observer to LifecycleRegistry)

init {
    val listener : MyLocationListener
    onCreate { 
       listener = MyLocationListener()     
    }
    onStart { 
       listener.start()
    }
    onStop { 
       listener.stop()
    }
}
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    
    val service = MyService()
    onResume {
        service.resume()
    }
    onPause {
        service.pause()
    }
    
}

Check Lifecycle Status

check current lifecycle status and return true if current status is matched

if (isCreated()) { // Do something cooler }

ViewModel

From ViewModelProviders

val myViewModel = MyViewModel().asViewModel(this) // 'this' is AppCompatActivity
myViewModel.getUsers().initObserver(this) {
    user -> doSomething(user)
}

lifecyclin's People

Contributors

harrytylenol avatar datayja avatar

Watchers

James Cloos avatar  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.