1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+ package androidx.lifecycle
1617
17- package androidx.lifecycle ;
18-
19- import android.app.Activity;
20- import android.app.Application;
21- import android.os.Build;
22- import android.os.Bundle;
23-
24- import androidx.annotation.NonNull;
25- import androidx.annotation.Nullable;
26- import androidx.annotation.RequiresApi;
27- import androidx.annotation.RestrictTo;
18+ import android.app.Activity
19+ import android.app.Application
20+ import android.os.Build
21+ import android.os.Bundle
22+ import androidx.annotation.RequiresApi
23+ import androidx.annotation.RestrictTo
2824
2925/* *
3026 * Internal class that dispatches initialization events.
3127 *
3228 * @hide
3329 */
34- @SuppressWarnings({" UnknownNullness" , " deprecation" })
35- // TODO https://issuetracker.google.com/issues/112197238
30+ @Suppress(" DEPRECATION" )
3631@RestrictTo(RestrictTo .Scope .LIBRARY_GROUP_PREFIX )
37- public class ReportFragment extends android.app.Fragment {
38- private static final String REPORT_FRAGMENT_TAG = " androidx.lifecycle"
39- + " .LifecycleDispatcher.report_fragment_tag" ;
40-
41- public static void injectIfNeededIn(Activity activity) {
42- if (Build .VERSION .SDK_INT >= 29 ) {
43- // On API 29+, we can register for the correct Lifecycle callbacks directly
44- LifecycleCallbacks .registerIn(activity);
45- }
46- // Prior to API 29 and to maintain compatibility with older versions of
47- // ProcessLifecycleOwner (which may not be updated when lifecycle-runtime is updated and
48- // need to support activities that don't extend from FragmentActivity from support lib),
49- // use a framework fragment to get the correct timing of Lifecycle events
50- android.app.FragmentManager manager = activity.getFragmentManager();
51- if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG ) == null ) {
52- manager.beginTransaction().add(new ReportFragment (), REPORT_FRAGMENT_TAG ).commit();
53- // Hopefully, we are the first to make a transaction.
54- manager.executePendingTransactions();
55- }
56- }
32+ open class ReportFragment () : android.app.Fragment() {
33+ private var processListener: ActivityInitializationListener ? = null
5734
58- @SuppressWarnings(" deprecation" )
59- static void dispatch(@NonNull Activity activity, @NonNull Lifecycle .Event event) {
60- if (activity instanceof LifecycleRegistryOwner ) {
61- ((LifecycleRegistryOwner ) activity).getLifecycle().handleLifecycleEvent(event);
62- return ;
63- }
64-
65- if (activity instanceof LifecycleOwner ) {
66- Lifecycle lifecycle = ((LifecycleOwner ) activity).getLifecycle();
67- if (lifecycle instanceof LifecycleRegistry ) {
68- ((LifecycleRegistry ) lifecycle).handleLifecycleEvent(event);
69- }
70- }
35+ private fun dispatchCreate (listener : ActivityInitializationListener ? ) {
36+ listener?.onCreate()
7137 }
7238
73- static ReportFragment get(Activity activity) {
74- return (ReportFragment ) activity.getFragmentManager().findFragmentByTag(
75- REPORT_FRAGMENT_TAG );
39+ private fun dispatchStart (listener : ActivityInitializationListener ? ) {
40+ listener?.onStart()
7641 }
7742
78- private ActivityInitializationListener mProcessListener;
79-
80- private void dispatchCreate(ActivityInitializationListener listener) {
81- if (listener != null ) {
82- listener.onCreate();
83- }
84- }
85-
86- private void dispatchStart(ActivityInitializationListener listener) {
87- if (listener != null ) {
88- listener.onStart();
89- }
90- }
91-
92- private void dispatchResume(ActivityInitializationListener listener) {
93- if (listener != null ) {
94- listener.onResume();
95- }
43+ private fun dispatchResume (listener : ActivityInitializationListener ? ) {
44+ listener?.onResume()
9645 }
9746
98- @Override
99- public void onActivityCreated(Bundle savedInstanceState) {
100- super .onActivityCreated(savedInstanceState);
101- dispatchCreate(mProcessListener);
102- dispatch(Lifecycle .Event .ON_CREATE );
47+ override fun onActivityCreated (savedInstanceState : Bundle ? ) {
48+ super .onActivityCreated(savedInstanceState)
49+ dispatchCreate(processListener)
50+ dispatch(Lifecycle .Event .ON_CREATE )
10351 }
10452
105- @Override
106- public void onStart() {
107- super .onStart();
108- dispatchStart(mProcessListener);
109- dispatch(Lifecycle .Event .ON_START );
53+ override fun onStart () {
54+ super .onStart()
55+ dispatchStart(processListener)
56+ dispatch(Lifecycle .Event .ON_START )
11057 }
11158
112- @Override
113- public void onResume() {
114- super .onResume();
115- dispatchResume(mProcessListener);
116- dispatch(Lifecycle .Event .ON_RESUME );
59+ override fun onResume () {
60+ super .onResume()
61+ dispatchResume(processListener)
62+ dispatch(Lifecycle .Event .ON_RESUME )
11763 }
11864
119- @Override
120- public void onPause() {
121- super .onPause();
122- dispatch(Lifecycle .Event .ON_PAUSE );
65+ override fun onPause () {
66+ super .onPause()
67+ dispatch(Lifecycle .Event .ON_PAUSE )
12368 }
12469
125- @Override
126- public void onStop() {
127- super .onStop();
128- dispatch(Lifecycle .Event .ON_STOP );
70+ override fun onStop () {
71+ super .onStop()
72+ dispatch(Lifecycle .Event .ON_STOP )
12973 }
13074
131- @Override
132- public void onDestroy() {
133- super .onDestroy();
134- dispatch(Lifecycle .Event .ON_DESTROY );
75+ override fun onDestroy () {
76+ super .onDestroy()
77+ dispatch(Lifecycle .Event .ON_DESTROY )
13578 // just want to be sure that we won't leak reference to an activity
136- mProcessListener = null ;
79+ processListener = null
13780 }
13881
139- private void dispatch(@NonNull Lifecycle .Event event ) {
82+ private fun dispatch (event : Lifecycle .Event ) {
14083 if (Build .VERSION .SDK_INT < 29 ) {
14184 // Only dispatch events from ReportFragment on API levels prior
14285 // to API 29. On API 29+, this is handled by the ActivityLifecycleCallbacks
14386 // added in ReportFragment.injectIfNeededIn
144- dispatch(getActivity() , event);
87+ dispatch(activity , event)
14588 }
14689 }
14790
148- void setProcessListener(ActivityInitializationListener processListener) {
149- mProcessListener = processListener;
91+ fun setProcessListener (processListener : ActivityInitializationListener ? ) {
92+ this .processListener = processListener
15093 }
15194
15295 interface ActivityInitializationListener {
153- void onCreate();
154-
155- void onStart();
156-
157- void onResume();
96+ fun onCreate ()
97+ fun onStart ()
98+ fun onResume ()
15899 }
159100
160101 // this class isn't inlined only because we need to add a proguard rule for it (b/142778206)
161102 // In addition to that registerIn method allows to avoid class verification failure,
162103 // because registerActivityLifecycleCallbacks is available only since api 29.
163104 @RequiresApi(29 )
164- static class LifecycleCallbacks implements Application .ActivityLifecycleCallbacks {
105+ internal class LifecycleCallbacks : Application .ActivityLifecycleCallbacks {
106+ override fun onActivityCreated (
107+ activity : Activity ,
108+ bundle : Bundle ?
109+ ) {}
165110
166- static void registerIn(Activity activity) {
167- activity.registerActivityLifecycleCallbacks(new LifecycleCallbacks ());
111+ override fun onActivityPostCreated (
112+ activity : Activity ,
113+ savedInstanceState : Bundle ?
114+ ) {
115+ dispatch(activity, Lifecycle .Event .ON_CREATE )
168116 }
169117
170- @Override
171- public void onActivityCreated(@NonNull Activity activity,
172- @Nullable Bundle bundle) {
173- }
118+ override fun onActivityStarted (activity : Activity ) {}
174119
175- @Override
176- public void onActivityPostCreated(@NonNull Activity activity,
177- @Nullable Bundle savedInstanceState) {
178- dispatch(activity, Lifecycle .Event .ON_CREATE );
120+ override fun onActivityPostStarted (activity : Activity ) {
121+ dispatch(activity, Lifecycle .Event .ON_START )
179122 }
180123
181- @Override
182- public void onActivityStarted(@NonNull Activity activity) {
183- }
124+ override fun onActivityResumed (activity : Activity ) {}
184125
185- @Override
186- public void onActivityPostStarted(@NonNull Activity activity) {
187- dispatch(activity, Lifecycle .Event .ON_START );
126+ override fun onActivityPostResumed (activity : Activity ) {
127+ dispatch(activity, Lifecycle .Event .ON_RESUME )
188128 }
189129
190- @Override
191- public void onActivityResumed(@NonNull Activity activity) {
130+ override fun onActivityPrePaused ( activity : Activity ) {
131+ dispatch( activity, Lifecycle . Event . ON_PAUSE )
192132 }
193133
194- @Override
195- public void onActivityPostResumed(@NonNull Activity activity) {
196- dispatch(activity, Lifecycle .Event .ON_RESUME );
197- }
134+ override fun onActivityPaused (activity : Activity ) {}
198135
199- @Override
200- public void onActivityPrePaused(@NonNull Activity activity) {
201- dispatch(activity, Lifecycle .Event .ON_PAUSE );
136+ override fun onActivityPreStopped (activity : Activity ) {
137+ dispatch(activity, Lifecycle .Event .ON_STOP )
202138 }
203139
204- @Override
205- public void onActivityPaused(@NonNull Activity activity) {
206- }
140+ override fun onActivityStopped (activity : Activity ) {}
207141
208- @Override
209- public void onActivityPreStopped(@NonNull Activity activity) {
210- dispatch(activity, Lifecycle . Event . ON_STOP );
211- }
142+ override fun onActivitySaveInstanceState (
143+ activity : Activity ,
144+ bundle : Bundle
145+ ) { }
212146
213- @Override
214- public void onActivityStopped(@NonNull Activity activity) {
147+ override fun onActivityPreDestroyed ( activity : Activity ) {
148+ dispatch( activity, Lifecycle . Event . ON_DESTROY )
215149 }
216150
217- @Override
218- public void onActivitySaveInstanceState(@NonNull Activity activity,
219- @NonNull Bundle bundle) {
151+ override fun onActivityDestroyed (activity : Activity ) {}
152+
153+ companion object {
154+ @JvmStatic
155+ fun registerIn (activity : Activity ) {
156+ activity.registerActivityLifecycleCallbacks(LifecycleCallbacks ())
157+ }
220158 }
159+ }
160+
161+ companion object {
162+ private const val REPORT_FRAGMENT_TAG =
163+ " androidx.lifecycle.LifecycleDispatcher.report_fragment_tag"
221164
222- @Override
223- public void onActivityPreDestroyed(@NonNull Activity activity) {
224- dispatch(activity, Lifecycle .Event .ON_DESTROY );
165+ @JvmStatic
166+ fun injectIfNeededIn (activity : Activity ) {
167+ if (Build .VERSION .SDK_INT >= 29 ) {
168+ // On API 29+, we can register for the correct Lifecycle callbacks directly
169+ LifecycleCallbacks .registerIn(activity)
170+ }
171+ // Prior to API 29 and to maintain compatibility with older versions of
172+ // ProcessLifecycleOwner (which may not be updated when lifecycle-runtime is updated and
173+ // need to support activities that don't extend from FragmentActivity from support lib),
174+ // use a framework fragment to get the correct timing of Lifecycle events
175+ val manager = activity.fragmentManager
176+ if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG ) == null ) {
177+ manager.beginTransaction().add(ReportFragment (), REPORT_FRAGMENT_TAG ).commit()
178+ // Hopefully, we are the first to make a transaction.
179+ manager.executePendingTransactions()
180+ }
225181 }
226182
227- @Override
228- public void onActivityDestroyed(@NonNull Activity activity) {
183+ @JvmStatic
184+ internal fun dispatch (activity : Activity , event : Lifecycle .Event ) {
185+ if (activity is LifecycleRegistryOwner ) {
186+ activity.lifecycle.handleLifecycleEvent(event)
187+ return
188+ }
189+ if (activity is LifecycleOwner ) {
190+ val lifecycle = (activity as LifecycleOwner ).lifecycle
191+ if (lifecycle is LifecycleRegistry ) {
192+ lifecycle.handleLifecycleEvent(event)
193+ }
194+ }
229195 }
196+
197+ @JvmStatic
198+ @get:JvmName(" get" )
199+ val Activity .reportFragment: ReportFragment
200+ get() {
201+ return this .fragmentManager.findFragmentByTag(
202+ REPORT_FRAGMENT_TAG
203+ ) as ReportFragment
204+ }
230205 }
231- }
206+ }
0 commit comments