This component provides a missing interface for Android’s Application lifecycle events and tools to more easily hook into Android’s activity lifecycle.
Add the dependency to your build.gradle
file
compile "com.github.InkApplications.DriveChain:lifecycle:2.+"
Add the Lifecycle module to your Application Component:
@Component(modules = arrayOf(
LifecycleModule::class
))
interface ApplicationComponent {
// ...
}
Since android does not have built in callbacks for the Application lifecycle, you will need to modify your application to invoke the interface directly.
DriveChain’s AppKernel manages dispatching your lifecycle events easily.
class MyApplication: Application() {
@Inject lateinit var appKernel: AppKernel
}
Next, make sure each of the lifecycle steps are passed along to the callbacks.
class MyApplication: Application() {
@Inject lateinit var appKernel: AppKernel
override fun onCreate() {
super.onCreate()
appKernel.onCreate(this)
}
override fun onTerminate() {
super.onTerminate()
appKernel.onTerminate(this)
}
override fun onLowMemory() {
super.onLowMemory()
appKernel.onLowMemory(this)
}
override fun onTrimMemory(level: Int) {
super.onTrimMemory(level)
appKernel.onTrimMemory(this, level)
}
}
Now, you can create a de-coupled lifecycle subscriber just by adding it to the set, and without
cluttering your Application class. With Dagger 2, this is as easy as annotating your
class with the @IntoSet
qualifier.
class WidgetInitializer: Initializer() {
override fun onCreate(application: Application) {
TODO()
}
}
And add it to your set using Dagger’s Multi-binding:
@Module class WidgetModule {
@Provides @IntoSet fun widget(): ApplicationLifecycleSubscriber = WidgetInitializer()
}
In rare cases, some libraries need to be able to interrupt the initialization of your application libraries. This typically occurs if the library needs to launch its own packaged activity without your application class logic. Lifecycle voters are invoked before the lifecycle subscribers, and have the ability to stop the normal lifecycle subscribers from being invoked.
You create a Lifecycle Voter similar to a Lifecycle Subscriber, however
each method will return a Boolean. Returning true
will proceed as normal,
but returning false
will prevent all lifecycle subscribers from being
invoked, stopping your application initialization.
class WidgetVoter: LifecycleVoter {
override fun onCreate(application: Application) = if (someCondition) true else false
override fun onLowMemory(application: Application) = true
override fun onTrimMemory(application: Application, level: Int) = true
override fun onTerminate(application: Application) = true
}
And add it to your set using Dagger’s Multi-binding:
@Module class WidgetModule {
@Provides @IntoSet fun widget(): LifecycleVoter = WidgetVoter()
}
Android Architecture components have already brought a great way to hook into the Activity lifecycle. You can use them in the same manner as the Application components
class MyActivity: AppCompatActivity {
@Inject lateinit var appKernel: AppKernel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
appKernel.bindLifecycle(lifecycle)
}
}