1、LifeCycle监听Activity生命周期
- 普通组件在使用过程中通常需要依赖于系统组件的生命周期,有时候,我们不得不在系统组件的生命周期回调方法中,主动去调用普通组件的方法,或者对其进行控制,因为普通组件无法获知系统组件生命周期事件。
传统写法
public class LocationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
//在页面创建时 初始化定位管理信息
initLocation();
}
@Override
protected void onResume() {
super.onResume();
///在页面显示时 开始定位
startLocation();
}
@Override
protected void onDestroy() {
super.onDestroy();
//在页面销毁时停止定位
stopLocation();
}
}
LifecycleObserver写法
- 继承LifecycleObserver
- @OnLifecycleEvent(Lifecycle.Event.ON_CREATE):注解的该方法在Activity的onCreate方法后会被调用
- @OnLifecycleEvent(Lifecycle.Event.ON_RESUME):注解的该方法在Activity的onResume方法后会被调用
- @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY):注解的该方法在Activity的onDestroy方法前会被调用
- MyLocationListener.java
package com.xinhe.testandroid;
import android.util.Log;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
public class MyLocationListener implements LifecycleObserver {
public static final String TAG = "Location";
/**
* 使用注解,使得在create时调用
*/
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void initLocation() {
Log.d(TAG, "initLocation");
}
/**
* 使用注解,使得在resume时调用
*/
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void startLocation() {
Log.d(TAG, "startLocation");
}
/**
* 使用注解,使得在destroy时调用
*/
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void stopLocation() {
Log.d(TAG, "stopLocation");
}
}
- getLifecycle().addObserver(myLocationListener);
- 只需要在Activity中注册定位对象,就可以在定位对象中,接收到Activity生命周期的变化通知
- MainActivity.java
package com.xinhe.testandroid;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Location";
public MyLocationListener myLocationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化定位对象
myLocationListener = new MyLocationListener();
getLifecycle().addObserver(myLocationListener);
Log.d(TAG,"onCreate");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
}
- 运行结果
- onCreate之后会自动调用initLocation
- onResume之后会自动调用startLocation
- stopLocation方法会在onDestroy方法之前调用
Location: onCreate
Location: initLocation
Location: onResume
Location: startLocation
// 退出应用后
Location: stopLocation
Location: onDestroy
2、LifeCycle监听Fragment生命周期
- 和Activity基本一致
public class BlankFragment extends Fragment {
private static final String TAG = "Fragment";
public MyLocationListener myLocationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate");
//初始化定位对象
myLocationListener = new MyLocationListener();
getLifecycle().addObserver(myLocationListener);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank, container, false);
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
}
3、LifeCycle监听Service生命周期
- 监听service的生命周期需要添加依赖
implementation ‘androidx.lifecycle:lifecycle-extensions:2.2.0‘
- 创建Service时,继承LifecycleService的MyService.java
public class MyService extends LifecycleService {
private static final String TAG = "MyServiceObserver";
private MyServiceObserver myServiceObserver;
public MyService(){
myServiceObserver = new MyServiceObserver();
getLifecycle().addObserver(myServiceObserver);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"onCreate");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
}
- MyServiceObserver.java
public class MyServiceObserver implements LifecycleObserver {
private static final String TAG = "MyServiceObserver";
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void startService(){
Log.d(TAG,"在MyServiceObserver中监听到Service启动 ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void stopService(){
Log.d(TAG,"在MyServiceObserver中监听到Service停止 ");
}
}
- MainActivity.java两个启动停止service按钮
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Location";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View view) {
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}
public void stop(View view) {
Intent intent = new Intent(MainActivity.this,MyService.class);
stopService(intent);
}
}
- 运行结果
- MyService是被观察者,MyServiceObserver是观察者,当被观察者MyService的生命周期发生变化时,回调观察者MyServiceObserver的指定方法
// startservice
MyServiceObserver: onCreate
MyServiceObserver: 在MyServiceObserver中监听到Service启动
// stopservice
MyServiceObserver: 在MyServiceObserver中监听到Service停止
MyServiceObserver: onDestroy
4、LifeCycle监听Application生命周期
- 监听app的生命周期需要添加依赖,和监听service的依赖相同
implementation ‘androidx.lifecycle:lifecycle-extensions:2.2.0‘
- MyApp.java
- ProcessLifecycleOwner
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(new ApplicatonOberver());
}
}
- ApplicatonOberver.java
package com.xinhe.testandroid;
import android.util.Log;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
class ApplicatonOberver implements LifecycleObserver {
private static final String TAG = "ApplicatonOberver";
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreate(){
Log.d(TAG,"onCreate in ApplicatonOberver");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart(){
Log.d(TAG,"onStart in ApplicatonOberver");
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume(){
Log.d(TAG,"onResume in ApplicatonOberver");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause(){
Log.d(TAG,"onPause in ApplicatonOberver");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop(){
Log.d(TAG,"onStop in ApplicatonOberver");
}
/**
* 该方法永远不会被调用,因为系统不会分发ON_DESTROY事件
*/
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy(){
Log.d(TAG,"onDestroy in ApplicatonOberver");
}
}
- 运行结果
APP刚启动时会依次执行以下三种方法
onCreate in ApplicatonOberver
onStart in ApplicatonOberver
onResume in ApplicatonOberver
APP切换到后台时会执行以下两种方法
onPause in ApplicatonOberver
onStop in ApplicatonOberver
APP再次切换到前台时会执行以下两种方法
onStart in ApplicatonOberver
onResume in ApplicatonOberver
APP退出
onPause in ApplicatonOberver
onStop in ApplicatonOberver
- 过ProcessLifecycleOwner我们可以轻易地检测到APP合适从前台进入到后台,又是何时从后台进入前台,进而可以进行一些业务操作,并且不会增加耦合度
- ProcessLifecycleOwner是针对整个应用程序的监听,与Activity无关。
- Lifecycle.Event.ON_CREATE只会调用一次,Lifecycle.Event.ON_DESTROY永远不会被调用。
- Lifecycle.Event.ON_START 和 Lifecycle.Event.ON_RESUME是一组,会依次调用,Lifecycle.Event.ON_START在Lifecycle.Event.ON_RESUME之前调用。
- Lifecycle.Event.ON_PAUSE 和 Lifecycle.Event.ON_STOP是一组,会依次调用,而且Lifecycle.Event.ON_PAUSE在Lifecycle.Event.ON_STOP之前调用。