当前位置:网站首页>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())
}
}
边栏推荐
- Requests Library
- 无需显示屏的VNC Viewer远程连接树莓派
- 水库大坝安全监测
- C程序设计专题 15-16年期末考试习题解答(上)
- 创意SVG环形时钟js特效
- Fast pace? high pressure? VR panoramic Inn brings you a comfortable life
- On the difficulty of developing large im instant messaging system
- Reservoir dam safety monitoring
- Eye gaze estimation using webcam
- 怎么把wps表格里某一列有重复项的整行删掉
猜你喜欢

On the difficulty of developing large im instant messaging system

Svg line animation background JS effect
Modstart: embrace new technologies and take the lead in supporting laravel 9.0

无需显示屏的VNC Viewer远程连接树莓派

融合模型权限管理设计方案

UE4 WebBrowser图表不能显示问题

JPA learning 1 - overview, JPA, JPA core annotations, JPA core objects

∞符号线条动画canvasjs特效
How to use promise Race() and promise any() ?
![[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching](/img/bd/b176e93ee6fa2125f021bcad3025c2.png)
[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching
随机推荐
无人驾驶: 对多传感器融合的一些思考
Design and practice of vivo server monitoring architecture
Collective example
Alternative to log4j
[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching
Current situation and development prospect forecast report of global and Chinese tetrahydrofurfuryl alcohol acetate industry from 2022 to 2028
Tutorial details | how to edit and set the navigation function in the coolman system?
【Proteus仿真】定时器0作为16位计数器使用示例
JDBC - database connection
Analysis report on development mode and investment direction of sodium lauriminodipropionate in the world and China 2022 ~ 2028
微搭低代码中实现增删改查
Collective例子
Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
Interesting checkbox counters
Reservoir dam safety monitoring
Is it so difficult to calculate the REM size of the web page according to the design draft?
The new employee of the Department after 00 is really a champion. He has worked for less than two years. The starting salary of 18K is close to me when he changes to our company
VIM use command
Report on operation pattern and future prospect of global and Chinese propyl isovalerate industry from 2022 to 2028
Svg line animation background JS effect
