基础03-Lifecycle

lifeCycle的Observer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public interface LifecycleObserver

public interface DefaultLifecycleObserver : LifecycleObserver {
/**
* Notifies that `ON_CREATE` event occurred.
*
*
* This method will be called after the [LifecycleOwner]'s `onCreate`
* method returns.
*
* @param owner the component, whose state was changed
*/
public fun onCreate(owner: LifecycleOwner) {}

/**
* Notifies that `ON_START` event occurred.
*
*
* This method will be called after the [LifecycleOwner]'s `onStart` method returns.
*
* @param owner the component, whose state was changed
*/
public fun onStart(owner: LifecycleOwner) {}

/**
* Notifies that `ON_RESUME` event occurred.
*
*
* This method will be called after the [LifecycleOwner]'s `onResume`
* method returns.
*
* @param owner the component, whose state was changed
*/
public fun onResume(owner: LifecycleOwner) {}

/**
* Notifies that `ON_PAUSE` event occurred.
*
*
* This method will be called before the [LifecycleOwner]'s `onPause` method
* is called.
*
* @param owner the component, whose state was changed
*/
public fun onPause(owner: LifecycleOwner) {}

/**
* Notifies that `ON_STOP` event occurred.
*
*
* This method will be called before the [LifecycleOwner]'s `onStop` method
* is called.
*
* @param owner the component, whose state was changed
*/
public fun onStop(owner: LifecycleOwner) {}

/**
* Notifies that `ON_DESTROY` event occurred.
*
*
* This method will be called before the [LifecycleOwner]'s `onDestroy` method
* is called.
*
* @param owner the component, whose state was changed
*/
public fun onDestroy(owner: LifecycleOwner) {}
}

public fun interface LifecycleEventObserver : LifecycleObserver {
/**
* Called when a state transition event happens.
*
* @param source The source of the event
* @param event The event
*/
public fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event)
}
  • 可以看到,我们在使用lifecycle.addObserver()时,可以传入LifecycleEventObserverDefaultLifecycleObserver,一个通过event获取当前状态,一个通过不同的回调函数获取当前状态

Activity树

1
2
3
4
5
6
7
8
9
10
11
Activity -- AccountAuthenticatorActivity
|- ActivityGroup -- TabActivity
|- ExpandableListActivity
|- LauncherActivity
|- ListActivity -- PreferenceActivity
|- NativeActivity
|- androidx.core.app.ComponentActivity -- androidx.activity.ComponentActivity -- FragmentActivity -- AppCompatActivity
|- PreviewActivity
|- BootstrapActivity
|- EmptyActivity
|- EmptyFloatingActivity

LifecycleOwner

1
2
3
4
5
6
7
8
public interface LifecycleOwner {
/**
* Returns the Lifecycle of the provider.
*
* @return The lifecycle of the provider.
*/
public val lifecycle: Lifecycle
}

androidx.core.app.ComponentActivityandroidx.activity.ComponentActivity都声明了对LifecycleOwner的实现

可以拿到lifecycleActivity

  • androidx.core.app.ComponentActivity
  • androidx.activity.ComponentActivity
  • FragmentActivity
  • AppCompatActivity
  • PreviewActivity

androidx.core.app.ComponentActivity中的lifecycle

1
2
3
4
5
6
7
8
9
@Suppress("LeakingThis")
private val lifecycleRegistry = LifecycleRegistry(this)
override val lifecycle: Lifecycle
get() = lifecycleRegistry
@CallSuper
override fun onSaveInstanceState(outState: Bundle) {
lifecycleRegistry.currentState = Lifecycle.State.CREATED
super.onSaveInstanceState(outState)
}
  • 很意外的是ComponentActivity并不是在每个生命周期回调函数中调用lifecycleRegistrysetCurrentState,从而分发生命周期

LifecycleRegistry

addObserver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
override fun addObserver(observer: LifecycleObserver) {
enforceMainThreadIfNeeded("addObserver")
val initialState = if (state == State.DESTROYED) State.DESTROYED else State.INITIALIZED
val statefulObserver = ObserverWithState(observer, initialState)
val previous = observerMap.putIfAbsent(observer, statefulObserver)
if (previous != null) {
return
}
val lifecycleOwner = lifecycleOwner.get()
?: // it is null we should be destroyed. Fallback quickly
return
val isReentrance = addingObserverCounter != 0 || handlingEvent
var targetState = calculateTargetState(observer)
addingObserverCounter++
while (statefulObserver.state < targetState && observerMap.contains(observer)
) {
pushParentState(statefulObserver.state)
val event = Event.upFrom(statefulObserver.state)
?: throw IllegalStateException("no event up from ${statefulObserver.state}")
statefulObserver.dispatchEvent(lifecycleOwner, event)
popParentState()
// mState / subling may have been changed recalculate
targetState = calculateTargetState(observer)
}
if (!isReentrance) {
// we do sync only on the top level.
sync()
}
addingObserverCounter--
}
  • 这里看到addObserver首先将observer包装成ObserverWithState,并将其加入到observerMap
  • 加入后立刻调用dispatchEvent将最新的状态传递出去
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private fun sync() {
val lifecycleOwner = lifecycleOwner.get()
?: throw IllegalStateException(
"LifecycleOwner of this LifecycleRegistry is already " +
"garbage collected. It is too late to change lifecycle state."
)
while (!isSynced) {
newEventOccurred = false
if (state < observerMap.eldest()!!.value.state) {
backwardPass(lifecycleOwner)
}
val newest = observerMap.newest()
if (!newEventOccurred && newest != null && state > newest.value.state) {
forwardPass(lifecycleOwner)
}
}
newEventOccurred = false
}
  • 这里看到sync会调用backwardPassforwardPass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
private fun forwardPass(lifecycleOwner: LifecycleOwner) {
@Suppress()
val ascendingIterator: Iterator<Map.Entry<LifecycleObserver, ObserverWithState>> =
observerMap.iteratorWithAdditions()
while (ascendingIterator.hasNext() && !newEventOccurred) {
val (key, observer) = ascendingIterator.next()
while (observer.state < state && !newEventOccurred && observerMap.contains(key)
) {
pushParentState(observer.state)
val event = Event.upFrom(observer.state)
?: throw IllegalStateException("no event up from ${observer.state}")
observer.dispatchEvent(lifecycleOwner, event)
popParentState()
}
}
}

private fun backwardPass(lifecycleOwner: LifecycleOwner) {
val descendingIterator = observerMap.descendingIterator()
while (descendingIterator.hasNext() && !newEventOccurred) {
val (key, observer) = descendingIterator.next()
while (observer.state > state && !newEventOccurred && observerMap.contains(key)
) {
val event = Event.downFrom(observer.state)
?: throw IllegalStateException("no event down from ${observer.state}")
pushParentState(event.targetState)
observer.dispatchEvent(lifecycleOwner, event)
popParentState()
}
}
}
  • 这里看到sync被调用后,就会根据observer的情况和state的不同,按照需要将state的变化传递给observer

ObserverWithState

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
internal class ObserverWithState(observer: LifecycleObserver?, initialState: State) {
var state: State
var lifecycleObserver: LifecycleEventObserver

init {
lifecycleObserver = Lifecycling.lifecycleEventObserver(observer!!)
state = initialState
}

fun dispatchEvent(owner: LifecycleOwner?, event: Event) {
val newState = event.targetState
state = min(state, newState)
lifecycleObserver.onStateChanged(owner!!, event)
state = newState
}
}
  • 首先这里看到一个关键的函数调用onStateChanged,也就是当dispatchEvent被调用时,state的改变就会传递给observer

setCurrentState作用

1
2
3
4
5
6
7
8
9
10
11
override var currentState: State
get() = state
/**
* Moves the Lifecycle to the given state and dispatches necessary events to the observers.
*
* @param state new state
*/
set(state) {
enforceMainThreadIfNeeded("setCurrentState")
moveToState(state)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private fun moveToState(next: State) {
if (state == next) {
return
}
check(!(state == State.INITIALIZED && next == State.DESTROYED)) {
"no event down from $state in component ${lifecycleOwner.get()}"
}
state = next
if (handlingEvent || addingObserverCounter != 0) {
newEventOccurred = true
// we will figure out what to do on upper level.
return
}
handlingEvent = true
sync()
handlingEvent = false
if (state == State.DESTROYED) {
observerMap = FastSafeIterableMap()
}
}
  • 可以看到这里对state进行一定逻辑判断后,调用了sync,也就是当前state的改变会传递给observer
  • activity传递state的方式看起来还是通过setCurrentState,但是并没有在CompinentActivity中的各个生命周期函数中调用

ObserverWithState如何处理两种LifecycleObserver

1
2
3
4
init {
lifecycleObserver = Lifecycling.lifecycleEventObserver(observer!!)
state = initialState
}

可以看到构造函数中将observer通过Lifecycling.lifecycleEventObserverobserver进行了包装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public fun lifecycleEventObserver(`object`: Any): LifecycleEventObserver {
val isLifecycleEventObserver = `object` is LifecycleEventObserver
val isDefaultLifecycleObserver = `object` is DefaultLifecycleObserver
if (isLifecycleEventObserver && isDefaultLifecycleObserver) {
return DefaultLifecycleObserverAdapter(
`object` as DefaultLifecycleObserver,
`object` as LifecycleEventObserver
)
}
if (isDefaultLifecycleObserver) {
return DefaultLifecycleObserverAdapter(`object` as DefaultLifecycleObserver, null)
}
if (isLifecycleEventObserver) {
return `object` as LifecycleEventObserver
}
val klass: Class<*> = `object`.javaClass
val type = getObserverConstructorType(klass)
if (type == GENERATED_CALLBACK) {
val constructors = classToAdapters[klass]!!
if (constructors.size == 1) {
val generatedAdapter = createGeneratedAdapter(
constructors[0], `object`
)
return SingleGeneratedAdapterObserver(generatedAdapter)
}
val adapters: Array<GeneratedAdapter> = Array(constructors.size) { i ->
createGeneratedAdapter(constructors[i], `object`)
}
return CompositeGeneratedAdaptersObserver(adapters)
}
return ReflectiveGenericLifecycleObserver(`object`)
}
  • 可以看到这里会判断observer的类型,如果是DefaultLifecycleObserver,则会使用DefaultLifecycleObserverAdapterobserver进行适配

  • 这里可以看到,适配器考虑到了Observer即是LifecycleEventObserver又是DefaultLifecycleObserver的情况

DefaultLifecycleObserverAdapter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
internal class DefaultLifecycleObserverAdapter(
private val defaultLifecycleObserver: DefaultLifecycleObserver,
private val lifecycleEventObserver: LifecycleEventObserver?
) : LifecycleEventObserver {
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
when (event) {
Lifecycle.Event.ON_CREATE -> defaultLifecycleObserver.onCreate(source)
Lifecycle.Event.ON_START -> defaultLifecycleObserver.onStart(source)
Lifecycle.Event.ON_RESUME -> defaultLifecycleObserver.onResume(source)
Lifecycle.Event.ON_PAUSE -> defaultLifecycleObserver.onPause(source)
Lifecycle.Event.ON_STOP -> defaultLifecycleObserver.onStop(source)
Lifecycle.Event.ON_DESTROY -> defaultLifecycleObserver.onDestroy(source)
Lifecycle.Event.ON_ANY ->
throw IllegalArgumentException("ON_ANY must not been send by anybody")
}
lifecycleEventObserver?.onStateChanged(source, event)
}
}
  • 这里就很清晰了,DefaultLifecycleObserverAdapter根据当前的state调用DefaultLifecycleObserver的各个回调

handleLifecycleEvent

1
2
3
4
5
6
7
8
9
10
11
12
/**
* Sets the current state and notifies the observers.
*
* Note that if the `currentState` is the same state as the last call to this method,
* calling this method has no effect.
*
* @param event The event that was received
*/
open fun handleLifecycleEvent(event: Event) {
enforceMainThreadIfNeeded("handleLifecycleEvent")
moveToState(event.targetState)
}
  • 这里看到handleLifecycleEvent也有设置state的作用

Activity生命周期相关函数

回调结构定义

1
2
private final ArrayList<Application.ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
new ArrayList<Application.ActivityLifecycleCallbacks>();

此处定义了一个回调的接口List

注册与删除

1
2
3
4
5
6
7
8
9
10
11
12
13
public void registerActivityLifecycleCallbacks(
@NonNull Application.ActivityLifecycleCallbacks callback) {
synchronized (mActivityLifecycleCallbacks) {
mActivityLifecycleCallbacks.add(callback);
}
}

public void unregisterActivityLifecycleCallbacks(
@NonNull Application.ActivityLifecycleCallbacks callback) {
synchronized (mActivityLifecycleCallbacks) {
mActivityLifecycleCallbacks.remove(callback);
}
}

回调list转数组

1
2
3
4
5
6
7
8
9
private Object[] collectActivityLifecycleCallbacks() {
Object[] callbacks = null;
synchronized (mActivityLifecycleCallbacks) {
if (mActivityLifecycleCallbacks.size() > 0) {
callbacks = mActivityLifecycleCallbacks.toArray();
}
}
return callbacks;
}
  • 下面介绍的dispatchActivityXXX函数将调用collectActivityLifecycleCallbacks获取所有callbacks,然后调用callbacksonActivityXXX函数

dispatchActivityPreCreated

1
2
3
4
5
6
7
8
9
10
private void dispatchActivityPreCreated(@Nullable Bundle savedInstanceState) {
getApplication().dispatchActivityPreCreated(this, savedInstanceState);
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i = 0; i < callbacks.length; i++) {
((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPreCreated(this,
savedInstanceState);
}
}
}

dispatchActivityCreated

1
2
3
4
5
6
7
8
9
10
private void dispatchActivityCreated(@Nullable Bundle savedInstanceState) {
getApplication().dispatchActivityCreated(this, savedInstanceState);
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i = 0; i < callbacks.length; i++) {
((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityCreated(this,
savedInstanceState);
}
}
}

dispatchActivityPostCreated

1
2
3
4
5
6
7
8
9
10
private void dispatchActivityPostCreated(@Nullable Bundle savedInstanceState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i = 0; i < callbacks.length; i++) {
((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityPostCreated(this,
savedInstanceState);
}
}
getApplication().dispatchActivityPostCreated(this, savedInstanceState);
}
  • 每段代码几乎是一样的,就不全贴上来了
  • 没贴上来的还有
    • dispatchActivityPreStarted, dispatchActivityStarted, dispatchActivityPostStarted
    • dispatchActivityPreResumed, dispatchActivityResumed, dispatchActivityPostResumed
    • dispatchActivityPrePaused, dispatchActivityPaused, dispatchActivityPostPaused
    • dispatchActivitySaveInstanceState, dispatchActivitySaveInstanceState, dispatchActivityPostSaveInstanceState
    • dispatchActivityPreDestroyed, dispatchActivityDestroyed, dispatchActivityPostDestroyed
    • dispatchActivityConfigurationChanged

ReportFragment

  • reportFragment负责传递Activity的生命周期

ReportFragment的初始化

1
2
3
4
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ReportFragment.injectIfNeededIn(this)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fun injectIfNeededIn(activity: Activity) {
if (Build.VERSION.SDK_INT >= 29) {
// On API 29+, we can register for the correct Lifecycle callbacks directly
LifecycleCallbacks.registerIn(activity)
}
// Prior to API 29 and to maintain compatibility with older versions of
// ProcessLifecycleOwner (which may not be updated when lifecycle-runtime is updated and
// need to support activities that don't extend from FragmentActivity from support lib),
// use a framework fragment to get the correct timing of Lifecycle events
val manager = activity.fragmentManager
if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) {
manager.beginTransaction().add(ReportFragment(), REPORT_FRAGMENT_TAG).commit()
// Hopefully, we are the first to make a transaction.
manager.executePendingTransactions()
}
}
  • api29之后,会使用LifecycleCallbacks.registerIn
  • 另外还会使用ReportFragment来报告activity的生命周期

ReportFragment分发生命周期(API 29之前)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
private var processListener: ActivityInitializationListener? = null

private fun dispatchCreate(listener: ActivityInitializationListener?) {
listener?.onCreate()
}

private fun dispatchStart(listener: ActivityInitializationListener?) {
listener?.onStart()
}

private fun dispatchResume(listener: ActivityInitializationListener?) {
listener?.onResume()
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
dispatchCreate(processListener)
dispatch(Lifecycle.Event.ON_CREATE)
}

override fun onStart() {
super.onStart()
dispatchStart(processListener)
dispatch(Lifecycle.Event.ON_START)
}

override fun onResume() {
super.onResume()
dispatchResume(processListener)
dispatch(Lifecycle.Event.ON_RESUME)
}

override fun onPause() {
super.onPause()
dispatch(Lifecycle.Event.ON_PAUSE)
}

override fun onStop() {
super.onStop()
dispatch(Lifecycle.Event.ON_STOP)
}

override fun onDestroy() {
super.onDestroy()
dispatch(Lifecycle.Event.ON_DESTROY)
// just want to be sure that we won't leak reference to an activity
processListener = null
}
  • 可以看到,当Fragment的声明周期函数被调用时,会调用dispatch,将activity的生命周期进行传递

dispatch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private fun dispatch(event: Lifecycle.Event) {
if (Build.VERSION.SDK_INT < 29) {
// Only dispatch events from ReportFragment on API levels prior
// to API 29. On API 29+, this is handled by the ActivityLifecycleCallbacks
// added in ReportFragment.injectIfNeededIn
dispatch(activity, event)
}
}

internal fun dispatch(activity: Activity, event: Lifecycle.Event) {
if (activity is LifecycleRegistryOwner) {
activity.lifecycle.handleLifecycleEvent(event)
return
}
if (activity is LifecycleOwner) {
val lifecycle = (activity as LifecycleOwner).lifecycle
if (lifecycle is LifecycleRegistry) {
lifecycle.handleLifecycleEvent(event)
}
}
}
  • 仅在api 29之前通过ReportFragment来进行activity的生命周期传递
  • 这里就可以看到上面分析的LifecycleRegistry类了,获取activity中的lifecycle再调用handleLifecycleEvent来更新activity的生命周期,进而将生命周期改变传递给listener

API 29及以后Activity分发生命周期

回到ReportFragment

1
2
3
4
5
6
7
fun injectIfNeededIn(activity: Activity) {
if (Build.VERSION.SDK_INT >= 29) {
// On API 29+, we can register for the correct Lifecycle callbacks directly
LifecycleCallbacks.registerIn(activity)
}
// ...
}

大于等于Api 29时,会调用registerIn

registerIn

1
2
3
4
@JvmStatic
fun registerIn(activity: Activity) {
activity.registerActivityLifecycleCallbacks(LifecycleCallbacks())
}
  • 这里看到,调用了activityregisterActivityLifecycleCallbacksactivity会在发生生命周期改变时调用回调LifecycleCallbacks

LifecycleCallbacks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
internal class LifecycleCallbacks : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(
activity: Activity,
bundle: Bundle?
) {}

override fun onActivityPostCreated(
activity: Activity,
savedInstanceState: Bundle?
) {
dispatch(activity, Lifecycle.Event.ON_CREATE)
}

override fun onActivityStarted(activity: Activity) {}

override fun onActivityPostStarted(activity: Activity) {
dispatch(activity, Lifecycle.Event.ON_START)
}

override fun onActivityResumed(activity: Activity) {}

override fun onActivityPostResumed(activity: Activity) {
dispatch(activity, Lifecycle.Event.ON_RESUME)
}

override fun onActivityPrePaused(activity: Activity) {
dispatch(activity, Lifecycle.Event.ON_PAUSE)
}

override fun onActivityPaused(activity: Activity) {}

override fun onActivityPreStopped(activity: Activity) {
dispatch(activity, Lifecycle.Event.ON_STOP)
}

override fun onActivityStopped(activity: Activity) {}

override fun onActivitySaveInstanceState(
activity: Activity,
bundle: Bundle
) {}

override fun onActivityPreDestroyed(activity: Activity) {
dispatch(activity, Lifecycle.Event.ON_DESTROY)
}

override fun onActivityDestroyed(activity: Activity) {}

companion object {
@JvmStatic
fun registerIn(activity: Activity) {
activity.registerActivityLifecycleCallbacks(LifecycleCallbacks())
}
}
}
  • 这里看到LifecycleCallbacks调用了dispatch()函数,将activity的生命周期进行了分发

Fragment的lifecycle

实现LifecycleOwner

1
2
3
4
5
6
7
8
public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener, LifecycleOwner,
ViewModelStoreOwner, HasDefaultViewModelProviderFactory, SavedStateRegistryOwner,
ActivityResultCaller {
@Override
@NonNull
public Lifecycle getLifecycle() {
return mLifecycleRegistry;
}

初始化Lifecycle

1
2
3
4
5
6
7
8
9
10
11
12
13
public Fragment() {
initLifecycle();
}
private void initLifecycle() {
mLifecycleRegistry = new LifecycleRegistry(this);
mSavedStateRegistryController = SavedStateRegistryController.create(this);
// The default factory depends on the SavedStateRegistry so it
// needs to be reset when the SavedStateRegistry is reset
mDefaultFactory = null;
if (!mOnPreAttachedListeners.contains(mSavedStateAttachListener)) {
registerOnPreAttachListener(mSavedStateAttachListener);
}
}

传递事件

fragment的生命周期传递比较简单,就是在fragment各个生命周期时调用handleLifecycleEvent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void performCreate(Bundle savedInstanceState) {
mChildFragmentManager.noteStateNotSaved();
mState = CREATED;
mCalled = false;
mLifecycleRegistry.addObserver(new LifecycleEventObserver() {
@Override
public void onStateChanged(@NonNull LifecycleOwner source,
@NonNull Lifecycle.Event event) {
if (event == Lifecycle.Event.ON_STOP) {
if (mView != null) {
mView.cancelPendingInputEvents();
}
}
}
});
onCreate(savedInstanceState);
mIsCreated = true;
if (!mCalled) {
throw new SuperNotCalledException("Fragment " + this
+ " did not call through to super.onCreate()");
}
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void performStart() {
mChildFragmentManager.noteStateNotSaved();
mChildFragmentManager.execPendingActions(true);
mState = STARTED;
mCalled = false;
onStart();
if (!mCalled) {
throw new SuperNotCalledException("Fragment " + this
+ " did not call through to super.onStart()");
}
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
if (mView != null) {
mViewLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_START);
}
mChildFragmentManager.dispatchStart();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void performResume() {
mChildFragmentManager.noteStateNotSaved();
mChildFragmentManager.execPendingActions(true);
mState = RESUMED;
mCalled = false;
onResume();
if (!mCalled) {
throw new SuperNotCalledException("Fragment " + this
+ " did not call through to super.onResume()");
}
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
if (mView != null) {
mViewLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
}
mChildFragmentManager.dispatchResume();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void performPause() {
mChildFragmentManager.dispatchPause();
if (mView != null) {
mViewLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
}
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
mState = AWAITING_ENTER_EFFECTS;
mCalled = false;
onPause();
if (!mCalled) {
throw new SuperNotCalledException("Fragment " + this
+ " did not call through to super.onPause()");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void performStop() {
mChildFragmentManager.dispatchStop();
if (mView != null) {
mViewLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
}
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
mState = ACTIVITY_CREATED;
mCalled = false;
onStop();
if (!mCalled) {
throw new SuperNotCalledException("Fragment " + this
+ " did not call through to super.onStop()");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
void performDestroy() {
mChildFragmentManager.dispatchDestroy();
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
mState = ATTACHED;
mCalled = false;
mIsCreated = false;
onDestroy();
if (!mCalled) {
throw new SuperNotCalledException("Fragment " + this
+ " did not call through to super.onDestroy()");
}
}
作者

Meow Meow Liu

发布于

2024-07-31

更新于

2024-08-19

许可协议

评论