preface
Besides activities, fragments, service and Application, LifeCycleService and ProccessLifeCycleOwner are described in this paper
A, LifeCycleService
public class LifecycleService extends Service implements LifecycleOwner {
// It can be used as an event distribution tool class to assist service event distribution
private final ServiceLifecycleDispatcher mDispatcher = new ServiceLifecycleDispatcher(this);
/ / Oncreate trigger event distribution - call mDispatcher. OnServicePreSuperOnCreate ()
@CallSuper
@Override
public void onCreate(a) {
mDispatcher.onServicePreSuperOnCreate();
super.onCreate();
}
@CallSuper
@Nullable
@Override
/ / OnBind trigger event distribution - call mDispatcher. OnServicePreSuperOnBind ()
public IBinder onBind(@NonNull Intent intent) {
mDispatcher.onServicePreSuperOnBind();
return null;
}
@SuppressWarnings("deprecation")
@CallSuper
@Override
/ / OnBind trigger event distribution - call mDispatcher. OnServicePreSuperOnStart ()
public void onStart(@Nullable Intent intent, int startId) {
mDispatcher.onServicePreSuperOnStart();
super.onStart(intent, startId);
}
// There is no event distribution because onStart is called internally
@CallSuper
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@CallSuper
@Override
/ / OnBind trigger event distribution - call mDispatcher. OnServicePreSuperOnDestroy ()
public void onDestroy(a) {
mDispatcher.onServicePreSuperOnDestroy();
super.onDestroy();
}
@Override
@NonNull
// Implement lifeCycleOwner's getLifeCycle interface
public Lifecycle getLifecycle(a) {
returnmDispatcher.getLifecycle(); }}Copy the code
Can see LifecycleService lifeCycleOwner interface is achieved, as holders of life cycle, the life cycle of the distribution and return of LifeCycle is implemented by using ServiceLifecycleDispatcher, And ServiceLifecycleDispatcher is to initialize the good from the start;
We can see respectively in onCreate/onStart/onBind/onDestroy trigger event distribution, and the distributed what event? Let’s look at ServiceLifecycleDispatcher
public class ServiceLifecycleDispatcher {
// Maintain observers and events
private final LifecycleRegistry mRegistry;
// Assist in event distribution
private final Handler mHandler;
// Assign tasks
private DispatchRunnable mLastDispatchRunnable;
/ * * *@param provider {@link LifecycleOwner} for a service, usually it is a service itself
*/
public ServiceLifecycleDispatcher(@NonNull LifecycleOwner provider) {
mRegistry = new LifecycleRegistry(provider);
mHandler = new Handler();
}
private void postDispatchRunnable(Lifecycle.Event event) {
if(mLastDispatchRunnable ! =null) {
mLastDispatchRunnable.run();
}
mLastDispatchRunnable = new DispatchRunnable(mRegistry, event);
mHandler.postAtFrontOfQueue(mLastDispatchRunnable);
}
/ / service onCreate onServicePreSuperOnCreate distributed ON_CREATE event, in the service call super. Before onCreate
public void onServicePreSuperOnCreate(a) {
postDispatchRunnable(Lifecycle.Event.ON_CREATE);
}
//service onBind onServicePreSuperOnBind issues the ON_START event before the service calls super.onbind
public void onServicePreSuperOnBind(a) {
postDispatchRunnable(Lifecycle.Event.ON_START);
}
//service onStart/onStartCommand onServicePreSuperOnStart issues the ON_START event before the service calls super.onstart
public void onServicePreSuperOnStart(a) {
postDispatchRunnable(Lifecycle.Event.ON_START);
}
/ / service onDestroy onServicePreSuperOnDestroy distributed ON_STOP/ON_DESTROY event, in the service call super. OnDestroy before
public void onServicePreSuperOnDestroy(a) {
postDispatchRunnable(Lifecycle.Event.ON_STOP);
postDispatchRunnable(Lifecycle.Event.ON_DESTROY);
}
/ * * *@return {@link Lifecycle} for the given {@link LifecycleOwner}
*/
@NonNull
public Lifecycle getLifecycle(a) {
return mRegistry;
}
static class DispatchRunnable implements Runnable {
private final LifecycleRegistry mRegistry;
final Lifecycle.Event mEvent;
private boolean mWasExecuted = false;
DispatchRunnable(@NonNull LifecycleRegistry registry, Lifecycle.Event event) {
mRegistry = registry;
mEvent = event;
}
@Override
public void run(a) {
if(! mWasExecuted) { mRegistry.handleLifecycleEvent(mEvent); mWasExecuted =true; }}}}Copy the code
summary
LifeCycleService with ServiceLifecycleDispatcher for LifeCycle access and event distribution.
The distribution ON_CTEATE event is triggered in service. OnCreate
Service. The OnStart onStartCommand/OnBind triggers in distributing ON_START events
Service.onDestroy triggers the distribution of ON_STOP and ON_DESTROY events
Second, the ProcessLifecycleOwner
public class ProcessLifecycleOwner implements LifecycleOwner {
@VisibleForTesting
static final long TIMEOUT_MS = 700; //mls
// Record the number of activities of start
private int mStartedCounter = 0;
// Record the number of activities that resume
private int mResumedCounter = 0;
// Whether the application pause event is sent
private boolean mPauseSent = true;
// Whether the application Stop event was sent
private boolean mStopSent = true;
private Handler mHandler;
private final LifecycleRegistry mRegistry = new LifecycleRegistry(this);
// Event dispatch task
private Runnable mDelayedPauseRunnable = new Runnable() {
@Override
public void run(a) { dispatchPauseIfNeeded(); dispatchStopIfNeeded(); }};// The activity initialization listener only listens onCreate onStart onResume
ActivityInitializationListener mInitializationListener =
new ActivityInitializationListener() {
@Override
public void onCreate(a) {}@Override
public void onStart(a) {
activityStarted();
}
@Override
public void onResume(a) { activityResumed(); }};// Singleton implementation
private static final ProcessLifecycleOwner sInstance = new ProcessLifecycleOwner();
// Return a singleton
@NonNull
public static LifecycleOwner get(a) {
return sInstance;
}
// Associate the context and initialize it
static void init(Context context) {
sInstance.attach(context);
}
// Distribute application ON_START event The number of activities of start is equal to 1 and mStopSent is true
void activityStarted(a) {
mStartedCounter++;
if (mStartedCounter == 1 && mStopSent) {
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
mStopSent = false; }}If the number of resume activities is equal to 1 and mPanseSent is true, if mPauseSent is not true, remove the mDelayedPauseRunnable that will be sent
void activityResumed(a) {
mResumedCounter++;
if (mResumedCounter == 1) {
if (mPauseSent) {
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
mPauseSent = false;
} else{ mHandler.removeCallbacks(mDelayedPauseRunnable); }}}If mResumedCounter = 0, the mDelayedPauseRunnable task will be triggered 700 ms later
void activityPaused(a) {
mResumedCounter--;
if (mResumedCounter == 0) { mHandler.postDelayed(mDelayedPauseRunnable, TIMEOUT_MS); }}/ / stop
void activityStopped(a) {
mStartedCounter--;
dispatchStopIfNeeded();
}
// If mResumedCounter == 0, distribute the ON_PAUSE event
void dispatchPauseIfNeeded(a) {
if (mResumedCounter == 0) {
mPauseSent = true; mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); }}// If mStartedCounter == 0 and onPause is sent, the ON_STOP event is issued
void dispatchStopIfNeeded(a) {
if (mStartedCounter == 0 && mPauseSent) {
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
mStopSent = true; }}// Private constructor
private ProcessLifecycleOwner(a) {}/ / distribute the attach will trigger the onCreate event, and through the activity when API > = 29. When registerActivityLifecycleCallbacks perception acitivity life cycle, It is sensed at <29 by setProcessListener(mInitializationListener)
void attach(Context context) {
mHandler = new Handler();
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
Application app = (Application) context.getApplicationContext();
app.registerActivityLifecycleCallbacks(new EmptyActivityLifecycleCallbacks() {
@Override
public void onActivityPreCreated(@NonNull Activity activity,
@Nullable Bundle savedInstanceState) {
activity.registerActivityLifecycleCallbacks(new EmptyActivityLifecycleCallbacks() {
@Override
public void onActivityPostStarted(@NonNull Activity activity) {
activityStarted();
}
@Override
public void onActivityPostResumed(@NonNull Activity activity) { activityResumed(); }}); }@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT < 29) { ReportFragment.get(activity).setProcessListener(mInitializationListener); }}@Override
public void onActivityPaused(Activity activity) {
activityPaused();
}
@Override
public void onActivityStopped(Activity activity) { activityStopped(); }}); }@NonNull
@Override
public Lifecycle getLifecycle(a) {
returnmRegistry; }}// The ReportFragment will distribute the mProcessListener first and then the ON_CREATE distribution
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dispatchCreate(mProcessListener);
dispatch(Lifecycle.Event.ON_CREATE);
}
/ / the activity will undertake dispatchActivityPreCreated distribution first, then is the dispatchActivityCreated fragments, and then the activity's onCreate
@UnsupportedAppUsage
final void performCreate(Bundle icicle, PersistableBundle persistentState) {
dispatchActivityPreCreated(icicle);
// omit the code
mFragments.dispatchActivityCreated();
dispatchActivityPostCreated(icicle);
}
Copy the code