AirCycle

AirCycle - flexible binding of Android Activity lifecycle callbacks to fields

License

License

GroupId

GroupId

com.github.simonpercic
ArtifactId

ArtifactId

aircycle
Last Version

Last Version

1.2.0
Release Date

Release Date

Type

Type

aar
Description

Description

AirCycle
AirCycle - flexible binding of Android Activity lifecycle callbacks to fields
Project URL

Project URL

https://github.com/simonpercic/AirCycle
Source Code Management

Source Code Management

https://github.com/simonpercic/AirCycle

Download aircycle

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.simonpercic/aircycle/ -->
<dependency>
    <groupId>com.github.simonpercic</groupId>
    <artifactId>aircycle</artifactId>
    <version>1.2.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.simonpercic/aircycle/
implementation 'com.github.simonpercic:aircycle:1.2.0'
// https://jarcasting.com/artifacts/com.github.simonpercic/aircycle/
implementation ("com.github.simonpercic:aircycle:1.2.0")
'com.github.simonpercic:aircycle:aar:1.2.0'
<dependency org="com.github.simonpercic" name="aircycle" rev="1.2.0">
  <artifact name="aircycle" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.simonpercic', module='aircycle', version='1.2.0')
)
libraryDependencies += "com.github.simonpercic" % "aircycle" % "1.2.0"
[com.github.simonpercic/aircycle "1.2.0"]

Dependencies

compile (1)

Group / Artifact Type Version
com.github.simonpercic : aircycle-api jar 1.2.0

Project Modules

There are no modules declared in this project.

AirCycle

Binds Android Activity lifecycle callbacks to Activity fields annotated with @AirCycle.

Android Arsenal CircleCI Download ![Method count](https://img.shields.io/badge/Methods count-core: 2 | deps: 109-e91e63.svg)

Fields annotated with @AirCycle that are defined in an Activity will receive lifecycle callbacks of the enclosing Activity. The Activity does NOT need to implement any interface or extend any specific base class.

The field itself also does NOT need to implement any interface or extend any specific base class.

The binding class is generated in compile time using Java annotation processing, NO reflection is used at runtime.

Usage

public class MyActivity extends AppCompatActivity {

    @AirCycle LifecycleListener myListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        MyActivityAirCycle.bind(this); // generated by the AirCycle annotation processor
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        
        myListener = new LifecycleListener();
    }
}
public class LifecycleListener {

    // no arguments 
    public void onCreate() {
        // The bound MyActivity was created
    }

    // can also pass the Activity
    public void onCreate(MyActivity activity) {
        // The bound MyActivity was created
    }

    // or just a Bundle 
    public void onCreate(Bundle bundle) {
        // The bound MyActivity was created
    }

    // ...

    // ActivityLifecycleCallbacks method naming is also supported
    public void onActivityDestroyed(AppCompatActivity activity, Bundle bundle) {
        // The bound MyActivity was destroyed
    }

    // can pass the base Activity as well
    public void onDestroy(AppCompatActivity activity) {
        // The bound MyActivity was destroyed
    }
    
    // arguments order does not matter in listener methods
    public void onDestroy(Bundle bundle, AppCompatActivity activity) {
        // The bound MyActivity was destroyed
    }
}

AirCycle annotation processor will generate a binding class for the Activity, named <YourActivity>AirCycle with a static bind method.

You MUST call MyActivityAirCycle.bind(this) BEFORE calling super.onCreate(savedInstanceState), otherwise the bound listener will NOT receive the first onCreate call for the Activity.

Activity lifecycle callbacks

The following Activity lifecycle callbacks are supported:

  • onCreate() (with optional Bundle savedInstanceState argument)
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • onSaveInstanceState() (with optional Bundle outState argument)
  • onDestroy()

Listener

Fields annotated with @AirCycle can be plain Java classes or interfaces (POJOs) that have public lifecycle callback methods. Method name defines the bound Activity lifecycle callback.

lifecycle callback listener method alternative listener method optional Bundle arg?
onCreate() onCreate() onActivityCreated()
onStart() onStart() onActivityStarted() -
onResume() onResume() onActivityResumed() -
onPause() onPause() onActivityPaused() -
onStop() onStop() onActivityStopped() -
onSaveInstanceState() onSaveInstanceState() onActivitySaveInstanceState()
onDestroy() onDestroy() onActivityDestroyed() -

ALL listener methods can optionally have the bound Activity instance as the method argument. Unlike method names, argument names do NOT matter.

Listener callbacks

Listener callbacks are invoked on Android's main thread. Callbacks are invoked AFTER the respective method returns.

Built-in listeners

The following listener interfaces are bundled with the library:

ActivityLifecycleCallbacks

Since its method names are also supported by AirCycle, you can also use Application.ActivityLifecycleCallbacks as a bound listener.

Custom listeners

Any class or interface that has at least one listener method can be bound as an AirCycle listener.

Custom listener from the example app:

public class CustomListener {

    public void onCreate() {
        
    }

    public void onCreate(CustomListenerActivity activity) {
        
    }

    public void onCreate(Bundle bundle) {
        
    }

    public void onSaveInstanceState(AppCompatActivity activity, Bundle bundle) {
        
    }

    public void onActivitySaveInstanceState(Bundle bundle, Activity activity) {
        
    }
}

Multiple methods

As seen above, multiple methods are supported for the same lifecycle callback (either overriden with different arguments or using an alternative supported name). The order and naming of arguments is NOT important. No arguments are necessary for any lifecycle method.

Multiple listeners

A single Activity can have multiple fields bound to its lifecycle.

Multiple listeners Activity from the example app:

public class MultipleListenersActivity extends AppCompatActivity {

    @AirCycle ActivityAirCycleLogger airCycleLogger;
    @AirCycle final ActivityBundleAirCycleLogger bundleAirCycleLogger = new ActivityBundleAirCycleLogger();
    
    // ...
}

Configuration

Create an AirCycleConfig using the builder:

AirCycleConfig airCycleConfig = AirCycleConfig.builder()
    // options ...
    .build();

Apply when binding

You can pass an instance of AirCycleConfig to the generated <YourActivity>AirCycle bind method when binding and it will only apply to that instance of the Activity.

MyActivityAirCycle.bind(this, airCycleConfig);

Apply as a default config

Alternatively, you can set an AirCycleConfig as the app-wide default config and it will be applied to all the AirCycles that don't have another config applied when binding.

AirCycleDefaultConfig.setConfig(airCycleConfig);

Configuration options

  • passIntentBundleOnCreate(true|false) if true, it passes the Activity's starting Intent Extras Bundle only if its savedInstanceState is null in onCreate(), i.e. getIntent().getExtras() with null-checks. If false, it always passes the savedInstanceState. Defaults to false.

  • ignoreLifecycleCallback(int) ignore an Activity lifecycle callback. Pass an ActivityLifecycleEvent IntDef from ActivityLifecycle's constants.

Ignore Activity lifecycle callbacks

There are 3 options to ignore lifecycle callbacks:

  • @AirCycle annotation option ignore:
@AirCycle(ignore = {ActivityLifecycle.RESUME, ActivityLifecycle.PAUSE}) ActivityAirCycleLogger airCycleLogger;
  • annotate a listener method with @Ignore:
public class CustomListener {

    // ...

    @Ignore
    public void onStart() {
    
    }

    // ...
}
  • AirCycleConfig configuration option:
AirCycleConfig airCycleConfig = AirCycleConfig.builder()
    // ...
    .ignoreLifecycleCallback(ActivityLifecycle.RESUME)
    .ignoreLifecycleCallback(ActivityLifecycle.PAUSE)
    // ...
    .build();

How does it work?

AirCycle generates one binding class per-Activity containing 1 or more @AirCycle annotated field(s). Upon calling bind on a generated class passing the Activity instance, Application.ActivityLifecycleCallbacks are registered on the Activity's app. These are automatically unregistered for the Activity once onDestroy is called.

The listener callbacks are invoked only if the listener instance is not null and are dispatched only to their respective bound listener instances.

ProTip

Annotate your Dagger injected presenters to automatically bind them to the Activity lifecycle, i.e.

@AirCycle @Inject MyPresenter presenter;

Reflection-less

AirCycle uses compile time annotation processing to generate Activity binding classes, NO reflection is used at runtime.

ProGuard

Since all classes are generated in compile time and no reflection is used in runtime, you can safely use AirCycle with ProGuard.

Include in your project

buildscript {
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    compile 'com.github.simonpercic:aircycle:1.2.0'
    apt 'com.github.simonpercic:aircycle-compiler:1.2.0'
}

Since Android Gradle Plugin version 2.2.0

dependencies {
    compile 'com.github.simonpercic:aircycle:1.2.0'
    annotationProcessor 'com.github.simonpercic:aircycle-compiler:1.2.0'
}

Inspiration and motivation

Inspired by SoundCloud's LightCycle library. Kudos to those guys.

Compared to SoundCloud's library, AirCycle supports passing of Activity lifecycle callbacks without the need to extend from a specific base Activity. Additionally, the listener classes are completely flexible, without the need to extend from any class or interface. This enables more flexibility when developing and further promotes composition over inheritance.

The cost of this flexibility is only supporting Activity lifecycle callbacks included in Application.ActivityLifecycleCallbacks (since API level 14) and no support for Fragment lifecycle callbacks.

Change Log

See CHANGELOG.md

License

Open source, distributed under the MIT License. See LICENSE for details.

Versions

Version
1.2.0