原文链接
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
和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停止 "); } }