Fragment的Lifecycle使用

在Fragment中直接调用可以获取到Fragment当前的状态:

getLifecycle().getCurrentState()

该状态可以进行判断,比如,是否在某个状态后:

getLifecycle().getCurrentState().isAtLeast(xxx)

xxx处填写状态枚举类:

Lifecycle.State.INITIALIZED
Lifecycle.State.CREATED
Lifecycle.State.STARTED
Lifecycle.State.RESUMED
Lifecycle.State.DESTROYED

完整写法:

if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
	...
}

猜你喜欢

转载自blog.csdn.net/qq_17767113/article/details/83616175