Lifecycle decouples pages and components

Monitor the running time of the program without using lifecycle. Components and activities are highly coupled (in order to achieve in the background, do not calculate the running time of the program)

public class MainActivity extends AppCompatActivity {
    
    
    private static final String TAG ="MainActivity" ;
    Chronometer Chronometer;
    Long el=0L;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate:创建");
        Chronometer = findViewById(R.id.Chronometer);
    }
    @Override
    protected void onResume() {
    
    
        Log.d(TAG, "onResume: 可交互");
        super.onResume();
        Chronometer.setBase(SystemClock.elapsedRealtime()-el);
        Chronometer.start();
    }
    @Override
    protected void onStop() {
    
    
        Log.d(TAG, "onStop: 后台");
        super.onStop();
        el = SystemClock.elapsedRealtime() - Chronometer.getBase();
        Chronometer.stop();
    }
}

Using lifecycle,
we customize components, inherit chronometer and implement the methods in lifecycle.

public class MyChronometer extends Chronometer implements LifecycleObserver {
    
    
    long el=0L;
    public MyChronometer(Context context, AttributeSet attrs) {
    
    
        super(context, attrs);
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    private void startMeter(){
    
    
     setBase(SystemClock.elapsedRealtime()-el);
     start();
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
  private void stopMeter(){
    
    
        el=SystemClock.elapsedRealtime()-getBase();
        stop();
  }
}

in the activity

public class Chronometer02Activity extends AppCompatActivity {
    
    
   private MyChronometer chronometer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chronometer);
        chronometer =findViewById(R.id.Chronometer_2);
        getLifecycle().addObserver(chronometer);
    }
}

layout file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Chronometer02Activity">
    <com.example.lifecycletest01.MyChronometer
        android:id="@+id/Chronometer_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="34sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Guess you like

Origin blog.csdn.net/qq_43867812/article/details/127885650