https://github.com/llanox/case-study-lifecycle-aware-components-usage
How to use the new Google library lifecycle aware component to improve some aspects regarding with maintainability and reliability concerns for Android apps.
https://github.com/llanox/case-study-lifecycle-aware-components-usage
android components lifecycle-aware
Last synced: about 1 month ago
JSON representation
How to use the new Google library lifecycle aware component to improve some aspects regarding with maintainability and reliability concerns for Android apps.
- Host: GitHub
- URL: https://github.com/llanox/case-study-lifecycle-aware-components-usage
- Owner: llanox
- License: mit
- Created: 2018-02-10T15:39:20.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-10T16:50:22.000Z (over 8 years ago)
- Last Synced: 2025-10-09T10:22:50.760Z (9 months ago)
- Topics: android, components, lifecycle-aware
- Language: Java
- Homepage:
- Size: 82 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Case Study Where We Can Use Lifecycle Aware Components to Improve Our Apps
How to use the new Google library lifecycle aware component to improve some aspects regarding with maintainability and reliability concerns for Android apps.
## Example Dummy App Radio APP
As an example, Radio APP has several components. All these components need to trigger some process on specific lifecycle events. Currently, we can't be sure that the Android system is going to call the lifecycle callback's in order. So, if one of these components try to execute a process in a non-valid state we get an error and also to impact the reliability of the application. In addition, when the components make part of third party library who are using it needs to know what process to execute for each lifecycle callback and duplicate that code in each Activity/Fragment that needs be aware of executing these processes. So, that library becomes hard to maintain and use.
**Before**
https://github.com/llanox/case-study-lifecycle-aware-components-usage/blob/master/app/src/main/java/com/radio/llanox/radioapp/ui/RadioActivity.java
```Java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio);
analyticsComponent = new AnalyticsComponent();
errorTrackingComponent = new ErrorTrackingComponent();
networkComponent = new NetworkComponent();
radioPlayerComponent = new RadioPlayerComponent();
}
@Override
protected void onStart() {
super.onStart();
analyticsComponent.init();
errorTrackingComponent.start();
networkComponent.start();
radioPlayerComponent.allocate();
}
@Override
protected void onStop() {
super.onStop();
analyticsComponent.finish();
errorTrackingComponent.stop();
networkComponent.stop();
radioPlayerComponent.cleanUp();
}
```
**After**
https://github.com/llanox/case-study-lifecycle-aware-components-usage/blob/feature/lifecycle_aware_components/app/src/main/java/com/radio/llanox/radioapp/ui/RadioActivity.java
Making components lifecycle aware, we delegate all logic to the component itself. So, who's using one of these components only need to add them as observers to the current LifeCycle and don't care anymore about it. With that simple implementation, we've improved the maintainability and reliability of our app.
```Java
public class AnalyticsComponent implements LifecycleObserver{
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void init() {
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void finish() {
}
}
```
```Java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio);
getLifecycle().addObserver(new AnalyticsComponent());
getLifecycle().addObserver(new ErrorTrackingComponent());
getLifecycle().addObserver(new NetworkComponent());
getLifecycle().addObserver(new RadioPlayerComponent());
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
```