Android Lifecycle Services

Additional

Language
Java
Version
0.1.0 (Nov 19, 2017)
Created
Jun 15, 2017
Updated
Feb 13, 2018 (Retired)
Owner
Juan Coria (juanmc2005)
Contributor
Juan Coria (juanmc2005)
1
Activity
Badge
Generate
Download
Source code

Android Lifecycle Services

This is a tiny lifecycle aware dependency provider for Android. It's inspired in ViewModelProviders from Android Architecture Components and it relies on Service Tree to retain instances organized according to the structure and active components of your application.

Why should you use it?

  • No leaks: it will never hold on to any reference of your activities or fragments
  • Low memory footprint: it's smart about instance creation
  • Plug and play: it initializes itself upon first use
  • Lifecycle aware: every resource is automatically created and disposed for you as your activities and fragments get created and destroyed
  • No imposition of inheritance: no need to extend from an application, activity, fragment, or resource class, just use it without restrictions
  • Team player: it plays well with Dagger. Plus, it doesn't require any previous configuration
  • Light as air: only 26kb and 102 methods (Release 0.1.0)

Show me the code!

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // App singleton example. This instance will always be the same.
    SomeService service = LifecycleServices.of(getApplication())
            .provide(SomeService.class)
            .getOrCreate(SomeService::new);

    /*
     * Activity singleton example.
     * This instance will always be the same throughout this activity's lifecycle
     */
    MainViewModel viewModel = LifecycleServices.of(this)
            .provide(MainViewModel.class)
            .getOrCreate(() -> new MainViewModel("some custom string"));
}

Some things to note

  • If the screen rotates, all bound instances remain, but if the activity is destroyed, then all activity's services are disposed
  • LifecycleServices is used to retrieve a ServiceProvider instance associated to your Application, Activity or Fragment
  • Given a class, ServiceProvider provides an instance of a LifecycleService, which is an abstraction that manages the lifespan of the required object. ServiceProviders are managed instances too, so don't worry about keeping references to them, they don't get recreated for the same Application, Activity or Fragment
  • Finally, a LifecycleService will be able to get a managed instance for you, or it will create one using a ServiceBuilder that receives as a parameter. If you have more complex dependencies, you should use a well known DI library and just request the injector to be managed (An example using Dagger is shown below)
  • Although LifecycleServices provides a method to manually dispose the instances, it's highly recommended not to use it, as the library will do that for you

Please note that the intention is NOT to replace dependency injection.

Dagger example

@Inject MainViewModel viewModel;
@Inject SomeService service;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Manage the component and use it to inject your dependencies with your custom scopes
    LifecycleServices.of(MainActivity.this)
            .provide(ActivityComponent.class)
            .getOrCreate(DaggerActivityComponent.builder()
                    .appComponent(MyApplication.getComponent(this))::build)
            .inject(this);
            
    // Use your instances...
}

In this example, the application class handles the app component (it could have also used Lifecycle Services). We request a managed instance of an activity component and we use that to inject the dependencies. Note that this can be abstracted in a method of a parent class or in an injector helper object.

Efforts are still being made to make this integration even easier.

Installation

Add jitpack as a repository in your top-level build.gradle file

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

Add the Android Lifecycle Services dependency to your module-level build.gradle file

dependencies {
    ...
    compile 'com.github.juanmc2005:android-lifecycle-services:x.y.z'
    ...
}

License

Copyright 2017 Juan Manuel Coria

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.