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.1.3'
Features
Add User Defined LifecycleObserver to LifecycleRegistry
First you should define class which inherits LifecycleObserver
class SampleActivith : AppCompatActivity(), LifecycleRegistryOwner {
var myListener = MyListener()
// ...
}
// ...
class MyListener : LifecycleObserver { /**/ }
Before
lifecycle.addObserver(myListener)
After
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 {
onCreate { }
onResume { }
onPause { }
onDestory { }
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
onResume {
}
onPause {
}
}
Check Lifecycle Status
check current lifecycle status and return true if current status is matched
Before
if (lifecycle.currentState.isAtLeast(Lifecycle.State.CREATED)) {
// Do something
}
After
if (isCreated()) {
// Do something cooler
}