https://github.com/orange-opensource/android-activity-lifecycle
Provide to any class a way to listen to android activity lifecycle events
https://github.com/orange-opensource/android-activity-lifecycle
Last synced: 5 months ago
JSON representation
Provide to any class a way to listen to android activity lifecycle events
- Host: GitHub
- URL: https://github.com/orange-opensource/android-activity-lifecycle
- Owner: Orange-OpenSource
- License: apache-2.0
- Archived: true
- Created: 2015-10-09T07:19:35.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-10-15T09:43:57.000Z (over 10 years ago)
- Last Synced: 2025-05-07T05:04:02.593Z (about 1 year ago)
- Language: Java
- Size: 254 KB
- Stars: 4
- Watchers: 16
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE.txt
Awesome Lists containing this project
README
#Android Lifecycle
If you are not familiar to the lifecycle concept in android activities, please have a look [here](https://developer.android.com/training/basics/activity-lifecycle/index.html]).
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](https://developer.android.com/reference/android/app/Activity.html) 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:
```java
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:
```java
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:
```java
public class MyActivity extends Activity {
// only the activity business!
}
```
```java
// 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](https://travis-ci.org/Orange-OpenSource/android-activity-lifecycle)
#License
Copyright (C) 2015 Orange
[Apache License Version 2.0](https://www.apache.org/licenses/LICENSE-2.0.html)
#Authors
Christophe Maldivi & Stephane Coutant