当前位置:网站首页>Decoupling pages and components using lifecycle
Decoupling pages and components using lifecycle
2022-06-25 00:21:00 【BY-91】
List of articles
Lifecycle Principle introduction
Jetpack Two classes are provided for us ,LifecyleOwner( Observed ) and LifecyleObserver( The observer ), Monitor the page life cycle through observer mode .
Lifecycle Yes Activity Life cycle monitoring
- binding activity Life cycle of
class LifeCycleListener(val activity: Activity, private val onChangeListener: OnChangeListener) :LifecycleObserver {
companion object{
init {
Log.e("tagLifeCycleListener", ": perform " )
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private fun onCreated(){
onChangeListener.onChange(" Callback onCreated perform ")
Log.e("tagLifeCycleListener", ":onCreated perform " )
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private fun onResume(){
Log.e("tagLifeCycleListener", ":onResume perform " )
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
private fun onPause(){
Log.e("tagLifeCycleListener", ":onPause perform " )
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private fun onDestroy(){
Log.e("tagLifeCycleListener", ":onDestroy perform " )
}
interface OnChangeListener{
fun onChange(des:String)
}
}
- Bind the observer to the observed
Log.e(TAG, "onCreate: perform ", )
lifecycleListener = LifeCycleListener(this, object : LifeCycleListener.OnChangeListener {
override fun onChange(des: String) {
Log.e(TAG, " Accept to onChange: ")
}
})
//lifecycle Bind the observer to the observed , Solving component pair activity The dependency of life cycle
lifecycle.addObserver(lifecycleListener)
override fun onResume() {
super.onResume()
Log.e(TAG, "onResume: perform ", )
}
override fun onPause() {
super.onPause()
Log.e(TAG, "onPause: perform ", )
}
override fun onDestroy() {
super.onDestroy()
Log.e(TAG, "onDestroy: perform ", )
}

Lifecycle Yes Service Life cycle monitoring
- LifecycleObserver The observer
class MyServiceObserver :LifecycleObserver{
private val MyServiceObserverTAG = "MyServiceObserver"
@OnLifecycleEvent(Lifecycle.Event.ON_START)
private fun onStart(){
Log.e(MyServiceObserverTAG, "start: perform " )
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private fun onDestroy(){
Log.e(MyServiceObserverTAG, "onDestroy: perform " )
}
}
- LifecycleService The binding of
class MyService: LifecycleService() {
private var myServiceObserver:MyServiceObserver = MyServiceObserver()
init {
lifecycle.addObserver(myServiceObserver)
}
}
fun launchService(view: View) {
startService(Intent(this,MyService::class.java))
}
fun closeService(view: View) {
stopService(Intent(this,MyService::class.java))
}

ProcessLifecycleOwner monitor APP Front and back of
- App Listening in the foreground or background
class ApplicationObserver:LifecycleObserver {
private val ApplicationObserverTAG = "ApplicationObserver"
/**
* Listen to the application's onCreate Method , The entire lifecycle is called only once
*/
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private fun onCreated(){
Log.e(ApplicationObserverTAG, "Application onCreated: perform " )
}
/**
* Listen to the application call when it is in the foreground
*/
@OnLifecycleEvent(Lifecycle.Event.ON_START)
private fun onStart(){
Log.e(ApplicationObserverTAG, "Application onStart: Front desk Executive " )
}
/**
* Listen to the application call when it is in the foreground
*/
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private fun onResume(){
Log.e(ApplicationObserverTAG, "Application onResume: Front desk Executive " )
}
/**
* Listen for application calls in the background
*/
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
private fun onPause(){
Log.e(ApplicationObserverTAG, "Application onPause: The background to perform " )
}
/**
* Listen for application calls in the background
*/
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private fun onStop(){
Log.e(ApplicationObserverTAG, "Application onStop: The background to perform " )
}
/**
* Never call , The system does not distribute calls on_destroy event
*/
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private fun onDestroy(){
Log.e(ApplicationObserverTAG, "Application onDestroy: Not execute " )
}
}
class APP: Application() {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(ApplicationObserver())
}
}
边栏推荐
- Hibernate learning 2 - lazy loading (delayed loading), dynamic SQL parameters, caching
- JPA learning 2 - core annotation, annotation addition, deletion, modification and query, list query result return type, one to many, many to one, many to many
- What is test development? Can you find a job at this stage?
- Hyperledger Fabric 2. X dynamic update smart contract
- Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
- [proteus simulation] example of using timer 0 as a 16 bit counter
- Tape SVG animation JS effect
- [issue 25] face to face experience of golang Engineer in the rightmost social recruitment
- On the difficulty of developing large im instant messaging system
- 节奏快?压力大?VR全景客栈带你体验安逸生活
猜你喜欢

怎么把wps表格里某一列有重复项的整行删掉

JDBC - database connection

∞ symbol line animation canvasjs special effect

颜色渐变梯度颜色集合
More pictures | explain the Nacos parameters in detail!

Creative SVG ring clock JS effect

The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply

UE4 WebBrowser图表不能显示问题

Eye gaze estimation using webcam
WordPress add photo album function [advanced custom fields Pro custom fields plug-in series tutorial]
随机推荐
On the difficulty of developing large im instant messaging system
[interview question] the difference between instancof and getclass()
Analysis report on operation pattern and supply and demand situation of global and Chinese cyano ketoprofen industry from 2022 to 2028
Overview of medium and low speed aerospace electronic bus
What is the difference between one way and two way ANOVA analysis, and how to use SPSS or prism for statistical analysis
Tape SVG animation JS effect
Power application of 5g DTU wireless communication module
C program design topic 15-16 final exam exercise solutions (Part 1)
节奏快?压力大?VR全景客栈带你体验安逸生活
融合模型权限管理设计方案
Intensive reading of thinking about markdown
Discrete mathematics and its application detailed explanation of exercises in the final exam of spring and summer semester of 2018-2019 academic year
部门新来的00后真是卷王,工作没两年,跳槽到我们公司起薪18K都快接近我了
VR全景怎么赚钱?结合市场从两个方面客观分析下
C# Winform 最大化遮挡任务栏和全屏显示问题
Color gradient gradient color collection
Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download
Common redis commands in Linux system
Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
Go crawler framework -colly actual combat (IV) -- Zhihu answer crawl (I)
