1.关于ActivityLifecycleCallbacks
API 14之后,在Application类中,提供了一个应用生命周期回调的注册方法,用来对应用的生命周期进行集中管理,这个接口叫registerActivityLifecycleCallbacks,可以通过它注册自己的ActivityLifeCycleCallback,每一个Activity的生命周期都会回调到这里的对应方法。之前我们想做类似限制制定Activity个数的时候都要自己去添加和计数,有了ActivityLifeCycleCallback接口,所有Activity的生命周期都会在这里回调,我们可以根据条件随心处理。
使用方式:
1.自定义一个ActivityObserver 去实现 Application.ActivityLifecycleCallbacks
public class ActivityObserver implements Application.ActivityLifecycleCallbacks {
public final String TAG=this.getClass().getSimpleName();
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Log.e(TAG,"onActivityCreated"+"===="+activity.getLocalClassName());
}
@Override
public void onActivityStarted(Activity activity) {
Log.e(TAG,"onActivityStarted"+"===="+activity.getLocalClassName());
}
@Override
public void onActivityResumed(Activity activity) {
Log.e(TAG,"onActivityResumed"+"===="+activity.getLocalClassName());
}
@Override
public void onActivityPaused(Activity activity) {
Log.e(TAG,"onActivityPaused"+"===="+activity.getLocalClassName());
}
@Override
public void onActivityStopped(Activity activity) {
Log.e(TAG,"onActivityStopped"+"===="+activity.getLocalClassName());
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
Log.e(TAG,"onActivitySaveInstanceState"+"===="+activity.getLocalClassName());
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.e(TAG,"onActivityDestroyed"+"===="+activity.getLocalClassName());
}
}
2.使用方式
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityObserver());
}
}
2.关于LifecycleObserver
Lifecycle是一个持有组件生命周期状态(如Activity或Fragment)的信息的类,并允许其他对象观察此状态。
使用方式:
1.自定义一个类实现LifecycleObserver接口,并重写实现的方法
public class MyLifeObserver implements LifecycleObserver {
public final String TAG=getClass().getSimpleName();
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny(){
Log.e(TAG,"onAny");
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreate(){
Log.e(TAG,"onCreate");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart(){
Log.e(TAG,"onStart");
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume(){
Log.e(TAG,"onResume");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause(){
Log.e(TAG,"onPause");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop(){
Log.e(TAG,"onStop");
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy(){
Log.e(TAG,"onDestroy");
}
}
2.使用方式
在activity的生命周期里面onCreate里面,注意这个地方activity需要需要实现LifecycleOwner这个接口。不过我们若使用activity继承AppCompatActivity的话 就不需要自己去实现,因为父类已经实现了这个方法,我们直接调用就是了。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
MyLifeObserver myLifeObserver=new MyLifeObserver();
getLifecycle().addObserver(myLifeObserver);
Log.e("tag","========onCreate");
super.onCreate(savedInstanceState);
}
activity里面的部分代码
@Override
protected void onStart() {
Log.e("tag","========onStart");
super.onStart();
Log.e("tag","========onStart2");
}
@Override
protected void onResume() {
Log.e("tag","========onResume");
super.onResume();
Log.e("tag","========onResume2");
}
最后启动app 查看logo日志
image.png
网友评论