package Ġorg . dee j dev . s cop el ib . lifecycle Ċ Ċ import Ġandroid . app . Activity Ċ import Ġandroid . app . Application Ċ import Ġandroid . os . Bundle Ċ import Ġandroidx . activity . Component Activity Ċ import Ġandroidx . collection . Simple Array Map Ċ import Ġandroidx . lifecycle . Lifecycle Ċ import Ġandroidx . lifecycle . Lifecycle Event Observer Ċ import Ġandroidx . lifecycle . Lifecycle Owner Ċ import Ġandroidx . lifecycle . Lifecycle Registry Ċ import Ġorg . dee j dev . s cop el ib . internal . unique InstanceId Ċ import Ġjava . lang . ref . Weak Reference Ċ import Ġjava . util .* Ċ Ċ private Ġval Ġlifecycle Owner By UniqueId Ġ= ĠSimple Array Map < String , ĠExtended Activity Lifecycle Owner >() Ċ Ċ private Ġclass ĠExtended Activity Lifecycle Owner Ġ: ĠLifecycle Owner Ġ{ ĊĠĠĠ Ġprivate Ġval Ġlifecycle Registry Ġ= ĠLifecycle Registry ( this ) ĊĠĠĠ Ġoverride Ġfun Ġget Lifecycle (): ĠLifecycle Ġ= Ġlifecycle Registry ĊĠĠĠ Ġfun Ġhandle Lifecycle Event ( event : ĠLifecycle . Event ) Ġ= Ġlifecycle Registry . handle Lifecycle Event ( event ) Ċ } Ċ Ċ internal Ġclass ĠExtended Activity Lifecycle Tracker Ġprivate Ġconstructor ( activity : ĠComponent Activity , ĠsavedInstanceState : ĠBundle ?) Ġ: ĠLifecycle Event Observer , ĠApplication . Activity Lifecycle Callbacks Ġ{ ĊĠĠĠ Ġprivate Ġval Ġactivity Ġ= ĠWeak Reference ( activity ) ĊĠĠĠ Ġprivate Ġval Ġunique Id : ĠString Ġ= ĠsavedInstanceState ?. unique InstanceId Ġ?: ĠUUID . randomUUID (). toString () ĊĊĠĠĠ Ġinit Ġ{ ĊĠĠĠĠĠĠĠ Ġensure Entry Exists () ĊĠĠĠĠĠĠĠ Ġactivity . application . register Activity Lifecycle Callbacks ( this ) ĊĠĠĠĠĠĠĠ Ġactivity . lifecycle . add Observer ( this ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Activity Save InstanceState ( activity : ĠActivity , Ġout State : ĠBundle ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( activity Ġ!= Ġthis . activity . get ()) Ġreturn ĊĊĠĠĠĠĠĠĠ Ġout State . unique InstanceId Ġ= Ġunique Id ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Activity Destroyed ( activity : ĠActivity ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( activity Ġ!= Ġthis . activity . get ()) Ġreturn ĊĊĠĠĠĠĠĠĠ Ġactivity . application . unregister Activity Lifecycle Callbacks ( this ) ĊĊĠĠĠĠĠĠĠ Ġif Ġ( activity . is Finish ing ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlifecycle Owner By UniqueId [ unique Id ]?. handle Lifecycle Event ( Lifecycle . Event . ON _ DESTROY ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlifecycle Owner By UniqueId . remove ( unique Id ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon StateChanged ( source : ĠLifecycle Owner , Ġevent : ĠLifecycle . Event ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( event Ġ!= ĠLifecycle . Event . ON _ DESTROY ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlifecycle Owner By UniqueId [ unique Id ]?. handle Lifecycle Event ( event ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġensure Entry Exists () Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ(! lifecycle Owner By UniqueId . containsKey ( unique Id )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlifecycle Owner By UniqueId . put ( unique Id , ĠExtended Activity Lifecycle Owner ()) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Activity Created ( activity : ĠActivity , ĠsavedInstanceState : ĠBundle ?) Ġ= ĠUnit ĊĠĠĠ Ġoverride Ġfun Ġon Activity Started ( activity : ĠActivity ) Ġ= ĠUnit ĊĠĠĠ Ġoverride Ġfun Ġon Activity Res umed ( activity : ĠActivity ) Ġ= ĠUnit ĊĠĠĠ Ġoverride Ġfun Ġon Activity Paused ( activity : ĠActivity ) Ġ= ĠUnit ĊĠĠĠ Ġoverride Ġfun Ġon Activity Stopped ( activity : ĠActivity ) Ġ= ĠUnit ĊĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġ@ Jvm Static ĊĠĠĠĠĠĠĠ Ġfun Ġregister ( activity : ĠComponent Activity , ĠsavedInstanceState : ĠBundle ?): ĠLifecycle Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġtracker Ġ= ĠExtended Activity Lifecycle Tracker ( activity , ĠsavedInstanceState ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġactivity . lifecycle . add Observer ( tracker ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġlifecycle Owner By UniqueId [ tracker . unique Id ] !!. lifecycle ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ