setMaxLifecycle 的使用方法
2021-09-20 本文已影响0人
菜鸟何时起飞
demo地址:
https://github.com/newPersonKing/SetMaxLifecycle_TestFragmentState.git
声明周期执行的几种流程
1 设置为STARTED
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.add(R.id.fl_main, fragment)
beginTransaction.setMaxLifecycle(fragment,Lifecycle.State.STARTED)
beginTransaction.commit()
1.1 fragment 的声明周期只能执行到 OnStart
1.2 切换到后台 直接执行 onStop
1.3 再切换到前台, 直接执行onStart
2 设置为CREATED
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.add(R.id.fl_main, fragment)
beginTransaction.setMaxLifecycle(fragment,Lifecycle.State.CREATED)
beginTransaction.commit()
2.1 声明周期只走到onCreate
2.2 切换到后台 不执行方法
2.3 切换到 前台 不执行方法
3 设置为 RESUMED
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.add(R.id.fl_main, fragment)
beginTransaction.setMaxLifecycle(fragment,Lifecycle.State.RESUMED)
beginTransaction.commit()
3.1 声明周期流程跟正常的流程一致
4 STARTED -> CREATED
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.add(R.id.fl_main, fragment)
beginTransaction.setMaxLifecycle(fragment,Lifecycle.State.STARTED)
beginTransaction.commit()
btn_main_frg_1.setOnClickListener {
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.setMaxLifecycle(fragment, androidx.lifecycle.Lifecycle.State.CREATED)
beginTransaction.commitNow()
}
4.1 立即执行 onStop -> onDestroyView
4.2 之后行为跟直接设置CREATED一致
5 RESUMED -> CREATED
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.add(R.id.fl_main, fragment)
beginTransaction.setMaxLifecycle(fragment,Lifecycle.State.RESUMED)
beginTransaction.commit()
btn_main_frg_1.setOnClickListener {
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.setMaxLifecycle(fragment, androidx.lifecycle.Lifecycle.State.CREATED)
beginTransaction.commitNow()
}
5.1 立即执行 onPause->onStop -> onDestroyView
5.2 之后行为跟直接设置CREATED一致
6 CREATED -> STARTED
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.add(R.id.fl_main, fragment)
beginTransaction.setMaxLifecycle(fragment,Lifecycle.State.CREATED)
beginTransaction.commit()
btn_main_frg_1.setOnClickListener {
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.setMaxLifecycle(fragment, androidx.lifecycle.Lifecycle.State.STARTED)
beginTransaction.commitNow()
}
6.1 立即执行 onCreateView->onActivityCreated -> onStart
6.2 之后行为跟直接设置STARTED一致
7 CREATED -> RESUMED
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.add(R.id.fl_main, fragment)
beginTransaction.setMaxLifecycle(fragment,Lifecycle.State.CREATED)
beginTransaction.commit()
btn_main_frg_1.setOnClickListener {
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.setMaxLifecycle(fragment, androidx.lifecycle.Lifecycle.State.RESUMED)
beginTransaction.commitNow()
}
7.1 立即执行 onCreateView->onActivityCreated -> onStart -> onResume
7.2 之后行为跟直接设置RESUMED一致
8 STARTED -> RESUMED
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.add(R.id.fl_main, fragment)
beginTransaction.setMaxLifecycle(fragment,Lifecycle.State.STARTED)
beginTransaction.commit()
btn_main_frg_1.setOnClickListener {
val beginTransaction = supportFragmentManager.beginTransaction()
beginTransaction.setMaxLifecycle(fragment, androidx.lifecycle.Lifecycle.State.RESUMED)
beginTransaction.commitNow()
}
8.1 立即执行 onResume
8.2 之后行为跟直接设置RESUMED一致