聲明 : http://www.reibang.com/p/714062a9af75
目錄
簡(jiǎn)介
原理
使用方法1,Activty 中使用
2,Fragment 中使用
3,service 中使用
4,Application監(jiān)聽 app 的聲明周期
?? 簡(jiǎn)介:
??LifeCycle可以幫助開發(fā)者創(chuàng)建可感知生命周期的組件,這樣, 組件便能夠在內(nèi)部管理自己的聲明周期,從而降低模塊間的耦合度,并降低內(nèi)存泄漏發(fā)生的可能性.
?? 工作原理:
??Jetpack 為我們提供了兩個(gè)類,LifecycleOwner(被觀察者)和 LifecycleObserver(觀察者).即通過觀察者模式,實(shí)現(xiàn)對(duì)頁面聲明周期的監(jiān)聽.通過源碼可以看出來,Activity 已經(jīng)默認(rèn)實(shí)現(xiàn)了 LifecycleOwner 接口,并提供了一個(gè) getLifecycle(LiftcycleObserver observer)方法,LifecycleOwner 正是通過該方法實(shí)現(xiàn)觀察者模式的.
@RestrictTo(LIBRARY_GROUP_PREFIX)
public class ComponentActivity extends Activity implements
LifecycleOwner,
KeyEventDispatcher.Component {
.......
/**
* This is only used for apps that have not switched to Fragments 1.1.0, where this
* behavior is provided by <code>androidx.activity.ComponentActivity</code>.
*/
private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
......
@CallSuper
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
mLifecycleRegistry.markState(Lifecycle.State.CREATED);
super.onSaveInstanceState(outState);
}
......
}
?? 使用方法:
1,Activty 中使用:
class MainActivity : AppCompatActivity() {
companion object {
const val Tag = "MainActivity"
}
var myLifeCycle = MyLifeCycle()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//將觀察者和被觀察者綁定~~~
lifecycle.addObserver(myLifeCycle)
Log.i(Tag, "MainAc---OnCreate")
}
override fun onStart() {
super.onStart()
Log.i(Tag, "MainAc---onStart")
}
//........雷同代碼不再貼出來
}
MyLifeCycle
class MyLifeCycle : LifecycleObserver {
companion object {
const val Tag = "MyLifeCycle"
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun myLifeOnCreate() {
Log.d(Tag, "MyLifeCycle---OnCreate")
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun myLifeonStart() {
Log.d(Tag, "MyLifeCycle---onStart")
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun myLifeonResume() {
Log.d(Tag, "MyLifeCycle---onResume")
}
//.....雷同代碼補(bǔ)貼出來了
}
運(yùn)行一下
是不是超級(jí)簡(jiǎn)單啊,????就這樣,LifeCycle 就完美的解決了組件對(duì)生命周期的依賴問題,使組件能夠管理自己的生命周期,降低耦合度.如果與 Activity 數(shù)據(jù)有交互,那就寫一個(gè)接口,回調(diào)一下的啦~~懶了,不想寫代碼了,當(dāng)然了Fragment 同樣的使用方法,看下面
2,Fragment 中使用
class HomeFragment : Fragment() {
var myLifeCycle = MyLifeCycle()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
myLifeCycle = MyLifeCycle()
lifecycle.addObserver(myLifeCycle)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
}
so easy~~~運(yùn)行結(jié)果截圖就不貼了哈.....因?yàn)閼?
3,service 中使用
Android提供了一個(gè)名為 LifecycleService 的類,該類繼承 Service,并實(shí)現(xiàn)了 LifecycleOwner 的接口,與 Activity/Fragment 類似,也提供了一個(gè) getLifecycle()的方法供我們使用.看下源碼
public class LifecycleService extends Service implements LifecycleOwner {
private final ServiceLifecycleDispatcher mDispatcher = new ServiceLifecycleDispatcher(this);
//.........
@Override
@NonNull
public Lifecycle getLifecycle() {
return mDispatcher.getLifecycle();
}
}
好了,開始實(shí)現(xiàn)吧.
在 build.gradle 中引用
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
然后開擼代碼
class MyService : LifecycleService() {
companion object {
const val Tag = "MyService"
}
init {
lifecycle.addObserver(MyLifeCycle())
}
override fun onCreate() {
super.onCreate()
Log.i(Tag, "MyService---OnCreate")
}
override fun onStart(intent: Intent?, startId: Int) {
super.onStart(intent, startId)
Log.i(Tag, "MyService---onStart")
}
override fun onDestroy() {
super.onDestroy()
Log.i(Tag, "MyService---onDestroy")
}
}
運(yùn)行一下
完活~
哦,別忘記,清單文件注冊(cè)一下服務(wù)
通過以上代碼可以看出來,當(dāng) Service 的聲明周期發(fā)生變化時(shí),不在需要主動(dòng)對(duì)組件進(jìn)行通知,LifecycleService很好的實(shí)現(xiàn)了組件與 Service 之間解耦.
4,Application監(jiān)聽 app 的聲明周期
LifeCycle 提供了一個(gè)名為 ProcessLifecycleOwner 的類,以便我們知道整個(gè)程序的聲明周期的情況.
Look下面.
build.gradle 文件
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
Application:
class MyApplication : Application() {
private val Tag = "MyApplication"
override fun onCreate() {
super.onCreate()
Log.i(Tag, "MyApplication---OnCreate")
ProcessLifecycleOwner.get().lifecycle.addObserver(MyLifeCycle())
}
}
MyLifeCycle:
重點(diǎn)看注釋,哪些會(huì)被調(diào)用,哪些不會(huì)被調(diào)用
class MyLifeCycle : LifecycleObserver {
companion object {
const val Tag = "MyLifeCycle"
}
//在應(yīng)用程序整個(gè)聲明周期中只會(huì)被調(diào)用一次
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun myLifeOnCreate() {
Log.d(Tag, "MyLifeCycle---OnCreate")
}
//當(dāng)應(yīng)用程序出現(xiàn)在前臺(tái)是被調(diào)用
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun myLifeonStart() {
Log.d(Tag, "MyLifeCycle---onStart")
}
//當(dāng)應(yīng)用程序出現(xiàn)在前臺(tái)是被調(diào)用
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun myLifeonResume() {
Log.d(Tag, "MyLifeCycle---onResume")
}
//當(dāng)應(yīng)用程序出現(xiàn)退出到后臺(tái)是被調(diào)用
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun myLifeonPause() {
Log.d(Tag, "MyLifeCycle---onPause")
}
//當(dāng)應(yīng)用程序出現(xiàn)退出到后臺(tái)是被調(diào)用
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun myLifeonStop() {
Log.d(Tag, "MyLifeCycle---onStop")
}
//永遠(yuǎn)不會(huì)被調(diào)用,系統(tǒng)不會(huì)分發(fā)調(diào)用ON_DESTROY事件
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun myLifeonDestroy() {
Log.d(Tag, "MyLifeCycle---onDestroy")
}
}
運(yùn)行一下
紅色框框里面的是,是我按了 home 鍵,回到桌面,然后又打開 app 的操作.
注意事項(xiàng)
??one: ProcessLifecycleOwner 是針對(duì)整個(gè)應(yīng)用程序的.
??two: Lifecycle.Event.ON_CREATE 只會(huì)被調(diào)用一次,Lifecycle.Event.ON_DESTROY永遠(yuǎn)不會(huì)調(diào)用.
??three: 當(dāng) app從后臺(tái)回到前臺(tái)時(shí),依次會(huì)走Lifecycle.Event.ON_START和Lifecycle.Event.ON_RESUME
??four: 當(dāng)app 從前臺(tái)回到后臺(tái)時(shí),會(huì)依次走Lifecycle.Event.ON_PAUSE和Lifecycle.Event.ON_STOP.
(((但是需要注意的是,這兩個(gè)調(diào)用有一定的延遲,因?yàn)橄到y(tǒng)需要為屏幕旋轉(zhuǎn),由于配置發(fā)生變化而導(dǎo)致 Acvtivty重新創(chuàng)建等情況預(yù)留一些時(shí)間,也就是說,系統(tǒng)需要保證當(dāng)設(shè)備出現(xiàn)這種情況時(shí),這兩個(gè)事件不會(huì)被調(diào)用,以為旋轉(zhuǎn)屏幕時(shí),app 沒有退到后臺(tái),只是進(jìn)入了橫屏/豎屏模式而已.)))
------->實(shí)不相瞞,括號(hào)里面的內(nèi)容我沒驗(yàn)證過,因?yàn)槲叶紡?qiáng)制豎屏或者變形,哈哈哈哈哈.??????