Anyone who’s starting to learn about Android development says lifecycle activities and fragments are a mystery.
As a reminder, here is the life cycle of the activities:

And that of the fragments is similar though a bit more complicated:

So, when we want somewhere in our code to perform an action when the activity is paused, for example, we must start the activity, overload the pause method, and perform this action, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class MainActivity extends AppCompatActivity { ... @Overrides public void onPause(){ locationManager.pause(); super.onPause(); } ... } |
And if we want to relaunch it at the time of the onResume, and the binder at the rest of the life cycle:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
public class MainActivity extends AppCompatActivity { private LocationManager locationManager; public void onCreate(Bundle savedInstanceState) { ... locationManager = new LocationManager(); } @Overrides public void onStart(){ super.onStart(); locationManager.start(); } @Overrides public void onStop(){ super.onStop(); locationManager.stop(); } @Overrides public void onPause(){ locationManager.pause(); super.onPause(); } @Overrides public void onResume(){ super.onResume(); locationManager.resume(); } @Overrides public void onDestroy(){ locationManager.destroy(); locationManager = null; super.onDestroy(); } } |
But let’s imagine now that our object rentalManager is contained in another object, for example, a Presenter, it would be necessary:
– To overload each method of our life cycle in our activity
– To create for each method its equivalent in presenting it
– In each of the methods of the presenter, connect it to that of the locationManager
We are therefore creating a multitude of useless methods.
Android Component Architecture: Lifecycle
Our friends at Google have redesigned this problem and offered us a solution: the Lifecycle !
It is now possible to recover in our activities and fragments a Lifecycleobject to which we can add listeners, and be notified of the change of state of our component.
For example, we can ask to be notified when the activity pauses, since our presentation, without the activity having to call a pause method of our present.
1. Installation
To take advantage of the architecture’s components, nothing simpler, use a version of appcompat-v7 greater than 26.1.0
1 |
compile "com.android.support:appcompat-v7:26.1.0" |
And use the fragments of the v4, as well as the AppCompatActivity (or FragmentActivity)
2. Use
Its use is very simple, on your Activity / Fragment, get the Lifecycle object with the getLifeCycle () method, then give it to the object that needs to be warned of the changes of states
1 2 3 4 5 6 7 8 9 10 11 |
public class MainActivity extends AppCompatActivity extends MainPresenter.View { private MainPresenter presenter; public void onCreate(Bundle savedInstanceState){ presenter = new MainPresenter(); presenter.setView(this); presenter.bindLifecycle(getLifeCycle()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class MainPresenter { private MainPresenter.View view; private LocationManager locationManager; public MainPresenter(){ locationManager = new LocationManager(); } public void bindLifecycle(LifeCycle lifecycle){@OnLifecycleEvent avec celui du lifecycle lifecycle.addObserver(this); locationManager.bindLifecycle(lifecycle); } @OnLifecycleEvent(ON_DESTROY) public void unbind(){ this.LocationManager = null; this.view = null; } @OnLifecycleEvent(ON_START) public void start(){ //votre code } public void setView(MainPresenter.View view){ this.view = view; } } |
There you go! I do not know about you but I can not do without this beautiful bookstore!
It allows you to cut the code by avoiding having to add to all our objects methods to share the life cycle everywhere, and it makes the code very clean.