Android Jetpack -- Lifecycles篇

Lifecycles In fact, from the name to see is certainly related to the life cycle, then it is what is associated with the life cycle?
First refer to the official document:
Lifecycles is a life-cycle-aware components, when will Activity or Fragment life cycle of change, Lifecycles will make the appropriate changes in the life cycle state, which holds information about component lifecycle state ( such activity or fragment thereof), and to allow other objects to observe this state.
It can be seen Lifecycles is a component that has a perceived life-cycle function, since it is a component, it shows that can be embedded in other places, such VewModel is one of them.

 

When we need to address the components associated with the life cycle, in the absence of Lifecycles offer, we need to set all kinds of callbacks, as follows:

 1 package com.example.lifecycle;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.os.Bundle;
 6 import android.util.Log;
 7 
 8 public class MainActivity extends AppCompatActivity {
 9 
10     String TAG = "myTag";
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16         Log.d(TAG, "onCreate: ");
17     }
18 
19     @Override
20     protected void onStart() {
21         super.onStart();
22         Log.d(TAG, "onStart: ");
23     }
24 
25     @Override
26     protected void onResume() {
27         super.onResume();
28         Log.d(TAG, "onResume: ");
29     }
30 
31     @Override
32     protected void onPause() {
33         super.onPause();
34         Log.d(TAG, "onPause: ");
35     }
36 
37     @Override
38     protected void onStop() {
39         super.onStop();
40         Log.d(TAG, "onStop: ");
41     }
42 
43     @Override
44     protected void onRestart() {
45         super.onRestart();
46         Log.d(TAG, "onRestart: ");
47     }
48 
49      @Override
 50      protected  void onDestroy () {
 51          super .onDestroy ();
52          Log.d (TAG, "onDestroy:" );
53      }
 54 }

When an excessive number of components of the time, you will need to manage a number of components in the lifecycle callbacks thereby inviting the amount of code, making the project difficult to maintain, and execute too many time-consuming operation can easily cause a memory leak in the life cycle, and these can be resolved through Lifecycles.

 

 

Examples of Use

Chronometer as an example to demonstrate to the lifecycle listeners

 1 public class MyChronometer extends Chronometer implements LifecycleObserver {
 2     private long time;
 3     public MyChronometer(Context context) {
 4         super(context);
 5     }
 6 
 7     public MyChronometer(Context context, AttributeSet attrs) {
 8         super(context, attrs);
 9     }
10 
11     public MyChronometer(Context context, AttributeSet attrs, int defStyleAttr) {
12         super(context, attrs, defStyleAttr);
13     }
14 
15     //以注解的形式完成监听
16     @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
17     private void stopMeter(){
18         time = SystemClock.elapsedRealtime() - getBase();
19         stop();
20     }
21 
22     @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
23     private void resumeMeter(){
24         setBase(SystemClock.elapsedRealtime() - time);
25         start();
26     }
27 }

Activity then as follows: the viewer can just add

 1 public class LifecyclesActivity extends AppCompatActivity {
 2 
 3     MyChronometer chronometer;
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_lifecycles);
 9 
10         chronometer = findViewById(R.id.meter);
11         getLifecycle().addObserver(chronometer);
12     }
13 
14 }

Activity and decoupling between a Chronometer, Chrononmeter themselves to achieve a good package, but also to facilitate the migration.
This is to bring the benefits of using Lifecycles.

Guess you like

Origin www.cnblogs.com/best-hym/p/12235446.html