1.Lifecycle
Lifecycle ,是被观察者,有addObserver和removeObserver这种添加或者移除观察者的方法
public abstract class Lifecycle {
AtomicReference<Object> mInternalScopeRef = new AtomicReference<>();
@MainThread
public abstract void addObserver(@NonNull LifecycleObserver observer);
@MainThread
public abstract void removeObserver(@NonNull LifecycleObserver observer);
@MainThread
@NonNull
public abstract State getCurrentState();
@SuppressWarnings("WeakerAccess")
public enum Event {
ON_CREATE,
ON_START,
ON_RESUME,
ON_PAUSE,
ON_STOP,
ON_DESTROY,
ON_ANY
}
/**
* Lifecycle states. You can consider the states as the nodes in a graph and
* {@link Event}s as the edges between these nodes.
*/
@SuppressWarnings("WeakerAccess")
public enum State {
/**
* Destroyed state for a LifecycleOwner. After this event, this Lifecycle will not dispatch
* any more events. For instance, for an {@link android.app.Activity}, this state is reached
* <b>right before</b> Activity's {@link android.app.Activity#onDestroy() onDestroy} call.
*/
DESTROYED,
/**
* Initialized state for a LifecycleOwner. For an {@link android.app.Activity}, this is
* the state when it is constructed but has not received
* {@link android.app.Activity#onCreate(android.os.Bundle) onCreate} yet.
*/
INITIALIZED,
/**
* Created state for a LifecycleOwner. For an {@link android.app.Activity}, this state
* is reached in two cases:
* <ul>
* <li>after {@link android.app.Activity#onCreate(android.os.Bundle) onCreate} call;
* <li><b>right before</b> {@link android.app.Activity#onStop() onStop} call.
* </ul>
*/
CREATED,
/**
* Started state for a LifecycleOwner. For an {@link android.app.Activity}, this state
* is reached in two cases:
* <ul>
* <li>after {@link android.app.Activity#onStart() onStart} call;
* <li><b>right before</b> {@link android.app.Activity#onPause() onPause} call.
* </ul>
*/
STARTED,
/**
* Resumed state for a LifecycleOwner. For an {@link android.app.Activity}, this state
* is reached after {@link android.app.Activity#onResume() onResume} is called.
*/
RESUMED;
/**
* Compares if this State is greater or equal to the given {@code state}.
*
* @param state State to compare with
* @return true if this State is greater or equal to the given {@code state}
*/
public boolean isAtLeast(@NonNull State state) {
return compareTo(state) >= 0;
}
}
}
2.LifecycleObserver
LifecycleObserver是观察者
public interface LifecycleObserver {
}
3.LifecycleOwner
LifecycleOwner是只有一个方法getLifecycle()的接口,是让拥有生命周期的东西实现比如(activity)用来获取Lifecycle。在Android Support Library 26.1.0 及其之后已经activity 和 fragment 已经默认实现了LifecycleOwner 所以在 activity 里我们可以直接:
getLifecycle().addObserver(new LocationListener());
这样我们的LocationListener就会感知 activity 的生命周期了
public interface LifecycleOwner {
/**
* Returns the Lifecycle of the provider.
*
* @return The lifecycle of the provider.
*/
@NonNull
Lifecycle getLifecycle();
}
4.绑定
当需要在activity启动后自动发起定位,退出后自动销毁定位可以这么做
- 观察者
public class LocationListener implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void startLocation() {
System.out.println("===startLocation===");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void stopLocation() {
System.out.println("===stopLocation===");
}
}
- 被观察者 在activity中添加代码
getLifecycle().addObserver(new LocationListener());
这样activity在进去和退出的时候输出日志
11-19 15:08:27.902 1629-1629/test.com.shineextest I/System.out: ===startLocation===
11-19 15:08:31.696 1629-1629/test.com.shineextest I/System.out: ===stopLocation===