俺啥也不晓得,莫得基础.看着怪新鲜的,就zun备试一哈
首先是这样
生命周期
可以看到 是通过注解 的
public class MyObserver implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void OnAnyListener() {
System.out.println("ON_ANY");
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void OnCreateListener() {
System.out.println("ON_CREATE");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void OnStartListener() {
System.out.println("ON_START");
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void OnResumeListener() {
System.out.println("ON_RESUME");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void OnPauseListener() {
System.out.println("ON_PAUSE");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void OnStopListener() {
System.out.println("ON_STOP");
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void OnDestoryListener() {
System.out.println("ON_DESTROY");
}
}
然而当我点进这个枚举
然后是这样
@SuppressWarnings("WeakerAccess")
public enum Event {
/**
* Constant for onCreate event of the {@link LifecycleOwner}.
*/
ON_CREATE,
/**
* Constant for onStart event of the {@link LifecycleOwner}.
*/
ON_START,
/**
* Constant for onResume event of the {@link LifecycleOwner}.
*/
ON_RESUME,
/**
* Constant for onPause event of the {@link LifecycleOwner}.
*/
ON_PAUSE,
/**
* Constant for onStop event of the {@link LifecycleOwner}.
*/
ON_STOP,
/**
* Constant for onDestroy event of the {@link LifecycleOwner}.
*/
ON_DESTROY,
/**
* An {@link Event Event} constant that can be used to match all events.
*/
ON_ANY
}
接着就那样
看的出来,也是需要先绑定的,然后关联
public class MainActivity extends AppCompatActivity implements LifecycleOwner {
private LifecycleRegistry lifecycleRegistry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lifecycleRegistry = new LifecycleRegistry(this);
lifecycleRegistry.addObserver(new MyObserver());
lifecycleRegistry.markState(Lifecycle.State.CREATED);
}
@Override
protected void onStart() {
super.onStart();
lifecycleRegistry.markState(Lifecycle.State.STARTED);
}
@Override
protected void onResume() {
super.onResume();
lifecycleRegistry.markState(Lifecycle.State.RESUMED);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
lifecycleRegistry.markState(Lifecycle.State.DESTROYED);
}
@Override
public Lifecycle getLifecycle() {
return lifecycleRegistry;
}
}
然而他的第二个枚举
明显少几个
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;
}
}
只开启Ac 打印结果 长的那些是 我退出了Ac
2019-03-07 11:16:22.225 21985-21985/? I/System.out: ON_CREATE
2019-03-07 11:16:22.225 21985-21985/? I/System.out: ON_ANY
2019-03-07 11:16:22.227 21985-21985/? I/System.out: ON_START
2019-03-07 11:16:22.227 21985-21985/? I/System.out: ON_ANY
2019-03-07 11:16:22.230 21985-21985/? I/System.out: ON_RESUME
2019-03-07 11:16:22.230 21985-21985/? I/System.out: ON_ANY
2019-03-07 11:16:36.976 21985-21985/com.as.lifecycle I/System.out: ON_PAUSE
2019-03-07 11:16:36.977 21985-21985/com.as.lifecycle I/System.out: ON_ANY
2019-03-07 11:16:37.213 21985-21985/com.as.lifecycle I/System.out: ON_STOP
2019-03-07 11:16:37.213 21985-21985/com.as.lifecycle I/System.out: ON_ANY
2019-03-07 11:16:37.214 21985-21985/com.as.lifecycle I/System.out: ON_DESTROY
2019-03-07 11:16:37.214 21985-21985/com.as.lifecycle I/System.out: ON_ANY
推测:这可能是观察者模式 给封了一个库 专门提供 组件 之间进行 沟通的
ON_ANY 会在每个方法之后在走一遍 然而 pause stop 是自动的 没有在Activty里mark 怪不得;俩个枚举少参数