当前位置:网站首页>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())
}
}
边栏推荐
- 技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放
- svg+js键盘控制路径
- 信号完整性(SI)电源完整性(PI)学习笔记(二十五)差分对与差分阻抗(五)
- Current situation analysis and development trend prediction report of hesperidase industry in the world and China from 2022 to 2028
- [leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching
- How can I persuade leaders to use DDD to construct the liver project?
- Meta&伯克利基于池化自注意力机制提出通用多尺度视觉Transformer,在ImageNet分类准确率达88.8%!开源...
- I suddenly find that the request dependent package in NPM has been discarded. What should I do?
- 无需显示屏的VNC Viewer远程连接树莓派
- 5-minute NLP: summary of 3 pre training libraries for rapid realization of NER
猜你喜欢
VR全景怎么赚钱?结合市场从两个方面客观分析下
svg+js键盘控制路径
Why do more and more physical stores use VR panorama? What are the advantages?
水库大坝安全监测
Tape SVG animation JS effect
[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching
Ten commandments of self-learning in machine learning
无人驾驶: 对多传感器融合的一些思考
How to delete the entire row with duplicate items in a column of WPS table
Svg+js keyboard control path
随机推荐
Svg+js keyboard control path
What is the difference between one way and two way ANOVA analysis, and how to use SPSS or prism for statistical analysis
D manual destruction may violate memory security
信号完整性(SI)电源完整性(PI)学习笔记(二十五)差分对与差分阻抗(五)
C# Winform 最大化遮挡任务栏和全屏显示问题
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
Domain Driven Design and coding
Analysis report on operation trend and investment strategy of global and Chinese tetrahydrofurfuryl propionate industry from 2022 to 2028
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
Go crawler framework -colly actual combat (IV) -- Zhihu answer crawl (I)
ArcGIS loads free online historical images as the base map (no plug-ins are required)
Alternative to log4j
DO280OpenShift访问控制--加密和ConfigMap
UE4 WebBrowser chart cannot display problems
Android SQLite database
Im instant messaging development application keeping alive process anti kill
Paper review: U2 net, u-net composed of u-net
磁带svg动画js特效
Design scheme of authority management of fusion model
Microsoft won the title of "leader" in the magic quadrant of Gartner industrial Internet of things platform again!