Jetpack架构组件(二)Lifecycle使用
程序员文章站
2022-06-01 10:37:52
1.直接添加如下依赖就可以满足日常的工作,如果缺少哪个库,再去单独添加就好了 添加这一句代码就依赖了如下的库。 2.先不谈Activity和Fragment中如何使用,先举一个Lifecycle的简单例子。 先实现MyObserver,对ON_CREATE和ON_RESUME事件进行监听。因为在An ......
1.直接添加如下依赖就可以满足日常的工作,如果缺少哪个库,再去单独添加就好了
implementation "android.arch.lifecycle:extensions:1.1.1"
1 ackage com.example.lifecycledemo1; 2 3 import android.arch.lifecycle.lifecycle; 4 import android.arch.lifecycle.lifecycleobserver; 5 import android.arch.lifecycle.onlifecycleevent; 6 import android.support.v7.app.appcompatactivity; 7 import android.os.bundle; 8 import android.util.log; 9 10 public class mainactivity extends appcompatactivity { 11 12 private static final string tag = "mainactivity"; 13 14 @override 15 protected void oncreate(bundle savedinstancestate) { 16 super.oncreate(savedinstancestate); 17 setcontentview(r.layout.activity_main); 18 getlifecycle().addobserver(new myobserver());//1 19 } 20 21 public class myobserver implements lifecycleobserver{ 22 23 @onlifecycleevent(lifecycle.event.on_resume) 24 void onresume(){ 25 log.d(tag, "lifecycle call onresume"); 26 } 27 @onlifecycleevent(lifecycle.event.on_pause) 28 void onpause(){ 29 log.d(tag, "lifecycle call onpause"); 30 } 31 } 32 33 @override 34 protected void onresume() { 35 super.onresume(); 36 log.d(tag, "onresume"); 37 } 38 39 @override 40 protected void onpause() { 41 super.onpause(); 42 log.d(tag, "onpause"); 43 44 } 45 }
先实现myobserver,对on_create和on_resume事件进行监听。因为在android support library 26.1.0 及其之后的版本,activity和fragment已经默认实现了lifecycleowner接口,所以在注释1处可以直接使用getlifecycle方法获取lifecycle对象,这样myobserver就可以观察mainactivity的生命周期变化了,lifecycleowner可以理解为被观察者,mainactivity默认实现了lifecycleowner接口,也就是说mainactivity是被观察者。
运行程序,打印的log如下所示。
d/mainactivity: onresume
d/mainactivity: lifecycle call onresume
d/mainactivity: lifecycle call onpause
d/mainactivity: onpause
3.mvp的例子
1 public class mypresenter implements ipresenter { 2 private static final string tag = "test"; 3 4 @override 5 public void onresume() { 6 log.d(tag, "lifecycle call onresume"); 7 } 8 9 @override 10 public void onpause() { 11 log.d(tag, "lifecycle call onpause"); 12 } 13 } 14 15 interface ipresenter extends lifecycleobserver { 16 17 @onlifecycleevent(lifecycle.event.on_resume) 18 void onresume(); 19 20 @onlifecycleevent(lifecycle.event.on_pause) 21 void onpause(); 22 }
ipresenter接口继承自lifecycleobserver接口,mypresenter又实现了ipresenter接口,这样mypresenter成为了一个观察者。
接在在mainactivity中加入mypresenter:
1 public class mainactivity extends appcompatactivity { 2 3 private static final string tag = "test"; 4 private ipresenter mpresenter; 5 @override 6 protected void oncreate(bundle savedinstancestate) { 7 super.oncreate(savedinstancestate); 8 setcontentview(r.layout.activity_main); 9 mpresenter = new mypresenter(); 10 getlifecycle().addobserver(mpresenter); 11 } 12 13 @override 14 protected void onresume() { 15 super.onresume(); 16 log.d(tag, "onresume"); 17 } 18 19 @override 20 protected void onpause() { 21 super.onpause(); 22 log.d(tag, "onpause"); 23 24 } 25 }
mainactivity成为了被观察者,当它的生命周期发生变化时,mypresenter就可以观察到,这样就不需要在mainactivity的多个生命周期方法中调用mypresenter的方法了。
打印的日志如下:
d/test: onresume
d/test: lifecycle call onresume
d/test: lifecycle call onpause
d/test: onpause