GithubHelp home page GithubHelp logo

isabella232 / android-activity-lifecycle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from orange-opensource/android-activity-lifecycle

0.0 0.0 0.0 260 KB

Provide to any class a way to listen to android activity lifecycle events

License: Apache License 2.0

Java 100.00%

android-activity-lifecycle's Introduction

#Android Lifecycle If you are not familiar to the lifecycle concept in android activities, please have a look here.

When you develop a real world application, with a lot of classes, there is a good chance that you need to do some stuff when activities' lifecycle events occur. For instance to release some resources or unregister from services when the "on destroy" event occurs.

It can lead to write what we call "boiler plate" code, since the event is notified through an Activity callback, and then you will have to forward this event to underlying objects through another mean. But let's give an example:

Your activity, which receives the "on destroy" event:

public class MyActivity extends Activity {
  private MyObject myObject;
  ...
  @Override
  public void onDestroy() {
    super.onDestroy();
    myObject.onDestroy();
  }
}

Then your object, which needs to release resources when the activity is destroyed:

class MyObject {
  private Runnable runnable;
  private Handler handler;
  ...
  void onDestroy() {
    handler.removeCallbacks(runnable);
  }
}

So here there is some room for improvment:

  • in our activity, we have to keep the myObject field only to forward the "on destroy" event to this object
  • in our activity, we have to override the onDestroy function only for this purpose as well
  • in our object we rely on MyActivity to provide the "on destroy" event

It would be great if we could break the strong link between these classes and that is the purpose of this library.

With the "android activity lifecycle" library, you can write:

public class MyActivity extends Activity {
  // only the activity business!
}
// implements Lifecycle interface to receive events
class MyObject implements Lifecycle {
  private Runnable runnable;
  private Handler handler;
  
  MyObject(Context context) {
    // Register for activity lifecycle events
    // with the context, which identifies the activity
    ApplicationLifecycle.register(this, context);
  }
  ...
  @Override
  public void onLifecycleEvent(LifecycleEvent lifecycleEvent) {
    if (lifecycleEvent == LifecycleEvent.ON_DESTROY) {
      // my business
      handler.removeCallbacks(runnable);
    }
  }
}

So here we can see that:

  • the activity source code is cleaner, and can focus on its own business
  • the object which needs to react to activity lifecyle events can handle its business by its own

We have reduce coupling between these objects.

#Build & use It is a maven build, so basically mvn install should do the job. It will install the library in your local repository. You can also retrieve the jar file under the target directory (after the maven build).

The library is published on maven central to ease developers' life. So to use the library in a maven build:

<dependency>
  <groupId>com.orange.android.activitylifecycle</groupId>
  <artifactId>android-activity-lifecycle</artifactId>
  <version>0.3</version>
  <scope>compile</scope>
</dependency>

In a gradle build:

dependencies {
  compile "com.orange.android.activitylifecycle:android-activity-lifecycle:0.3"
}

#TODO

  • describe the debug api
  • describe the test app
  • extend to service/broadcast receiver/content provider specific lifecyle behaviour

#Travis build See job

#License Copyright (C) 2015 Orange

Apache License Version 2.0

#Authors Christophe Maldivi & Stephane Coutant

android-activity-lifecycle's People

Contributors

christophemaldivi 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.