In daily development, some time-consuming tasks or objects need to be associated with some life cycles of Activity or Fragment to release resources or schedule tasks. Our usual practice is to execute the relevant code in the corresponding life cycle function, but this will lead to the need to write a lot of template code, and life cycle management is more troublesome.
In order to make App development more efficient and let developers focus more on business code, Google provides Lifecycle development component, which provides a convenient way of cycle management. Developers can develop more concise code and make life cycle management easier
1, What is Lifecycle
As we mentioned at the beginning, lifecycle is a set of component libraries provided by Google so that other objects can perceive and listen to the life cycle of the host (Activity and Fragment). Lifecycle mainly consists of three components
- LifeCycleOwner is the owner of the life cycle, or the host, i.e. Activity and Fragment
- LifeCycleObserver life cycle listener, which can be any class
- Lifecycle, which stores the host lifecycle information. Listeners can listen to this information
2, Why use Lifecycle
In daily development, we may encounter such a scenario. We need to start loading some data when the onResume method of Activity or Fragment is called (it may be other periodic functions that will be called during startup, such as onCreate of Activity). However, in order to avoid memory leakage, we need to stop loading data in the onDestroy method. The usual practice is
Task load class
package com.android.jetpackdemo class Task { fun start(){ } fun stop(){ } }
Associated Activity lifecycle
class MainActivity : AppCompatActivity() { val task:Task = Task() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } override fun onResume() { super.onResume() task.start() } override fun onDestroy() { super.onDestroy() task.stop() } }
In this way, we can certainly achieve the desired effect, but when we need to deal with a large number of tasks, we need to bind cumbersome and different codes in such periodic functions, just as we need to write many findviewbyids. And the business code of Activity will become very complex.
3, How to use Lifecycle
For example, in Section 2, we use the traditional method to bind the life cycle of data loading Task and Activity. Let's see how to use Lifecycle for transformation.
3.1. Add dependency
def lifecycle_version = "2.3.1" implementation 'androidx.appcompat:appcompat:1.2.0' implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
3.2. Transform the Task as a listener
You need to implement the interface Android X lifecycle. Lifecycleobserver, which is a methodless interface. And add the corresponding cycle annotation on the start and stop methods
class Task : LifecycleObserver { companion object{ const val TAG = "Task" } @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun start(){ Log.d(TAG,"start loading") } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) fun stop(){ Log.d(TAG,"stop loading") } }
3.3. Transform Activity
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) lifecycle.addObserver(Task()) } }
After transformation, our Activity will become very concise, with a lot of template code that has nothing to do with the Activity business.
Finally, when the corresponding periodic function is executed, the methods in the Task will also be executed accordingly
2021-05-22 22:29:39.694 20244-20244/com.android.jetpackdemo D/Task: start loading 2021-05-22 22:29:43.978 20244-20244/com.android.jetpackdemo D/Task: stop loading