当前位置:网站首页>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())
}
}
边栏推荐
- Modstart: embrace new technologies and take the lead in supporting laravel 9.0
- Svg+js keyboard control path
- Intensive reading of thinking about markdown
- 怎么把wps表格里某一列有重复项的整行删掉
- 无人驾驶: 对多传感器融合的一些思考
- 有趣的checkbox计数器
- 2019 summary and 2020 outlook
- 微搭低代码中实现增删改查
- ∞ symbol line animation canvasjs special effect
- 人体改造 VS 数字化身
猜你喜欢

有趣的checkbox计数器

Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181

Unimportant tokens can be stopped in advance! NVIDIA proposes an efficient visual transformer network a-vit with adaptive token to improve the throughput of the model

Im instant messaging development application keeping alive process anti kill

Svg+js keyboard control path

svg线条动画背景js特效

Ansible及playbook的相关操作

U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency
5-minute NLP: summary of 3 pre training libraries for rapid realization of NER

Zed acquisition
随机推荐
Zed acquisition
Fast pace? high pressure? VR panoramic Inn brings you a comfortable life
Ansible及playbook的相关操作
How to delete the entire row with duplicate items in a column of WPS table
Meta & Berkeley proposed a universal multi-scale visual transformer based on pooled self attention mechanism. The classification accuracy in Imagenet reached 88.8%! Open source
One way 和two way ANOVA分析的区别是啥,以及如何使用SPSS或者prism进行统计分析
canvas螺旋样式的动画js特效
What is test development? Can you find a job at this stage?
In the past 5 years, from "Diandian" to the current test development, my success is worth learning from.
Power application of 5g DTU wireless communication module
磁带svg动画js特效
A small program written this week
不重要的token可以提前停止计算!英伟达提出自适应token的高效视觉Transformer网络A-ViT,提高模型的吞吐量!...
Approaching harvest moon:moonbeam DFI Carnival
Current situation analysis and development trend prediction report of hesperidase industry in the world and China from 2022 to 2028
OTT营销之风正盛,商家到底该怎么投?
Meta&伯克利基于池化自注意力机制提出通用多尺度视觉Transformer,在ImageNet分类准确率达88.8%!开源...
Design and practice of vivo server monitoring architecture
December 6, 2019 what happens after the browser enters a URL
What are the advantages of VR panoramic production? Why is it favored?
