1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.lifecycle; 18 19 import android.app.Activity; 20 import android.app.Fragment; 21 import android.os.Bundle; 22 23 import androidx.annotation.RestrictTo; 24 25 /** 26 * Internal class that dispatches initialization events. 27 * 28 * @hide 29 */ 30 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 31 public class ReportFragment extends Fragment { 32 private static final String REPORT_FRAGMENT_TAG = "androidx.lifecycle" 33 + ".LifecycleDispatcher.report_fragment_tag"; 34 injectIfNeededIn(Activity activity)35 public static void injectIfNeededIn(Activity activity) { 36 // ProcessLifecycleOwner should always correctly work and some activities may not extend 37 // FragmentActivity from support lib, so we use framework fragments for activities 38 android.app.FragmentManager manager = activity.getFragmentManager(); 39 if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) { 40 manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit(); 41 // Hopefully, we are the first to make a transaction. 42 manager.executePendingTransactions(); 43 } 44 } 45 get(Activity activity)46 static ReportFragment get(Activity activity) { 47 return (ReportFragment) activity.getFragmentManager().findFragmentByTag( 48 REPORT_FRAGMENT_TAG); 49 } 50 51 private ActivityInitializationListener mProcessListener; 52 dispatchCreate(ActivityInitializationListener listener)53 private void dispatchCreate(ActivityInitializationListener listener) { 54 if (listener != null) { 55 listener.onCreate(); 56 } 57 } 58 dispatchStart(ActivityInitializationListener listener)59 private void dispatchStart(ActivityInitializationListener listener) { 60 if (listener != null) { 61 listener.onStart(); 62 } 63 } 64 dispatchResume(ActivityInitializationListener listener)65 private void dispatchResume(ActivityInitializationListener listener) { 66 if (listener != null) { 67 listener.onResume(); 68 } 69 } 70 71 @Override onActivityCreated(Bundle savedInstanceState)72 public void onActivityCreated(Bundle savedInstanceState) { 73 super.onActivityCreated(savedInstanceState); 74 dispatchCreate(mProcessListener); 75 dispatch(Lifecycle.Event.ON_CREATE); 76 } 77 78 @Override onStart()79 public void onStart() { 80 super.onStart(); 81 dispatchStart(mProcessListener); 82 dispatch(Lifecycle.Event.ON_START); 83 } 84 85 @Override onResume()86 public void onResume() { 87 super.onResume(); 88 dispatchResume(mProcessListener); 89 dispatch(Lifecycle.Event.ON_RESUME); 90 } 91 92 @Override onPause()93 public void onPause() { 94 super.onPause(); 95 dispatch(Lifecycle.Event.ON_PAUSE); 96 } 97 98 @Override onStop()99 public void onStop() { 100 super.onStop(); 101 dispatch(Lifecycle.Event.ON_STOP); 102 } 103 104 @Override onDestroy()105 public void onDestroy() { 106 super.onDestroy(); 107 dispatch(Lifecycle.Event.ON_DESTROY); 108 // just want to be sure that we won't leak reference to an activity 109 mProcessListener = null; 110 } 111 dispatch(Lifecycle.Event event)112 private void dispatch(Lifecycle.Event event) { 113 Activity activity = getActivity(); 114 if (activity instanceof LifecycleRegistryOwner) { 115 ((LifecycleRegistryOwner) activity).getLifecycle().handleLifecycleEvent(event); 116 return; 117 } 118 119 if (activity instanceof LifecycleOwner) { 120 Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle(); 121 if (lifecycle instanceof LifecycleRegistry) { 122 ((LifecycleRegistry) lifecycle).handleLifecycleEvent(event); 123 } 124 } 125 } 126 setProcessListener(ActivityInitializationListener processListener)127 void setProcessListener(ActivityInitializationListener processListener) { 128 mProcessListener = processListener; 129 } 130 131 interface ActivityInitializationListener { onCreate()132 void onCreate(); 133 onStart()134 void onStart(); 135 onResume()136 void onResume(); 137 } 138 } 139