本文整理了Java中androidx.lifecycle.Lifecycle
类的一些代码示例,展示了Lifecycle
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Lifecycle
类的具体详情如下:
包路径:androidx.lifecycle.Lifecycle
类名称:Lifecycle
[英]Defines an object that has an Android Lifecycle. android.support.v4.app.Fragmentand android.support.v4.app.FragmentActivity classes implement LifecycleOwner interface which has the LifecycleOwner#getLifecycle() method to access the Lifecycle. You can also implement LifecycleOwnerin your own classes.
Event#ON_CREATE, Event#ON_START, Event#ON_RESUME events in this class are dispatched after the LifecycleOwner's related method returns. Event#ON_PAUSE, Event#ON_STOP, Event#ON_DESTROY events in this class are dispatched before the LifecycleOwner's related method is called. For instance, Event#ON_START will be dispatched after android.app.Activity#onStart returns, Event#ON_STOP will be dispatched before android.app.Activity#onStop is called. This gives you certain guarantees on which state the owner is in.
If you use Java 8 Language, then observe events with DefaultLifecycleObserver. To include it you should add "android.arch.lifecycle:common-java8:" to your build.gradle file.
class TestObserver implements DefaultLifecycleObserver {
@Override
public void onCreate(LifecycleOwner owner) {
// your code
}
}
If you use Java 7 Language, Lifecycle events are observed using annotations. Once Java 8 Language becomes mainstream on Android, annotations will be deprecated, so between DefaultLifecycleObserver and annotations, you must always prefer DefaultLifecycleObserver.
class TestObserver implements LifecycleObserver {
@OnLifecycleEvent(ON_STOP)
void onStopped() {}
}
Observer methods can receive zero or one argument. If used, the first argument must be of type LifecycleOwner. Methods annotated with Event#ON_ANY can receive the second argument, which must be of type Event.
class TestObserver implements LifecycleObserver {
@OnLifecycleEvent(ON_CREATE)
void onCreated(LifecycleOwner source) {}
@OnLifecycleEvent(ON_ANY)
void onAny(LifecycleOwner source, Event event) {}
}
These additional parameters are provided to allow you to conveniently observe multiple providers and events without tracking them manually.
[中]定义具有Android生命周期的对象。安卓支持v4。应用程序。碎片和安卓。支持v4。应用程序。FragmentActivity类实现LifecycleOwner接口,该接口具有访问生命周期的LifecycleOwner#getLifecycle()方法。您还可以在自己的类中实现LifecycleOwner。
此类中的事件#ON#CREATE、事件#ON#START、事件#ON#RESUME事件在LifecycleOwner的相关方法返回后被调度。在调用LifecycleOwner的相关方法之前,将调度此类中的事件#ON_PAUSE、事件#ON_STOP、事件#ON#u DESTROY事件。例如,事件#ON_START将在android之后调度。应用程序。活动#启动返回,事件#启动停止将在android之前调度。应用程序。活动#在顶部被称为。这为您提供了所有者所处状态的某些保证。
如果使用Java 8语言,则使用DefaultLifecycleObserver观察事件。要包含它,您应该在构建中添加“android.arch.lifecycle:common-java8:”。格雷德尔档案。
class TestObserver implements DefaultLifecycleObserver {
@Override
public void onCreate(LifecycleOwner owner) {
// your code
}
}
如果您使用Java 7语言,则使用注释观察生命周期事件。一旦Java 8语言成为Android上的主流,注释将被弃用,因此在DefaultLifecycleObserver和注释之间,您必须始终选择DefaultLifecycleObserver
class TestObserver implements LifecycleObserver {
@OnLifecycleEvent(ON_STOP)
void onStopped() {}
}
观察者方法可以接收零个或一个参数。如果使用,第一个参数必须是LifecycleOwner类型。用Event#ON_ANY注释的方法可以接收第二个参数,该参数必须是Event类型。
class TestObserver implements LifecycleObserver {
@OnLifecycleEvent(ON_CREATE)
void onCreated(LifecycleOwner source) {}
@OnLifecycleEvent(ON_ANY)
void onAny(LifecycleOwner source, Event event) {}
}
这些附加参数允许您方便地观察多个提供者和事件,而无需手动跟踪它们。
代码示例来源:origin: trello/RxLifecycle
private AndroidLifecycle(LifecycleOwner owner) {
owner.getLifecycle().addObserver(this);
}
代码示例来源:origin: bluelinelabs/Conductor
public ArchLifecycleController() {
Log.i(TAG, "Conductor: Constructor called");
getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Event.ON_ANY)
void onLifecycleEvent(@NonNull LifecycleOwner source, @NonNull Event event) {
Log.d(TAG, "Lifecycle: " + source.getClass().getSimpleName() + " emitted event " + event + " and is now in state " + source.getLifecycle().getCurrentState());
}
});
Log.d(TAG, "Lifecycle: " + getClass().getSimpleName() + " is now in state " + getLifecycle().getCurrentState());
}
代码示例来源:origin: trello/RxLifecycle
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
void onEvent(LifecycleOwner owner, Lifecycle.Event event) {
lifecycleSubject.onNext(event);
if (event == Lifecycle.Event.ON_DESTROY) {
owner.getLifecycle().removeObserver(this);
}
}
}
代码示例来源:origin: chat-sdk/chat-sdk-android
public void setEnabled (boolean enabled) {
if (enabled == this.enabled) {
return;
}
this.enabled = enabled;
if(enabled) {
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
else {
ProcessLifecycleOwner.get().getLifecycle().removeObserver(this);
}
}
代码示例来源:origin: bluelinelabs/Conductor
@OnLifecycleEvent(Event.ON_ANY)
void onLifecycleEvent(@NonNull LifecycleOwner source, @NonNull Event event) {
Log.d(TAG, "Lifecycle: " + source.getClass().getSimpleName() + " emitted event " + event + " and is now in state " + source.getLifecycle().getCurrentState());
}
});
代码示例来源:origin: ankidroid/Anki-Android
@Override
protected void onPostExecute(Collection col) {
super.onPostExecute(col);
if (mLifecycleOwner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.CREATED)) {
mCallback.execute(col);
}
}
代码示例来源:origin: square/picasso
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.picasso_sample_activity);
sampleContent = findViewById(R.id.sample_content);
final ListView activityList = findViewById(R.id.activity_list);
final PicassoSampleAdapter adapter = new PicassoSampleAdapter(this);
activityList.setAdapter(adapter);
activityList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
adapter.getItem(position).launch(PicassoSampleActivity.this);
}
});
showHide = findViewById(R.id.faux_action_bar_control);
showHide.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
activityList.setVisibility(checked ? VISIBLE : GONE);
}
});
getLifecycle().addObserver(PicassoProvider.get());
}
代码示例来源:origin: xcesco/kripton
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
assertMainThread("observe");
if (owner.getLifecycle().getCurrentState() == DESTROYED) {
return;
owner.getLifecycle().addObserver(wrapper);
代码示例来源:origin: robolectric/robolectric
@Test
public void launch_lifecycleOwnerActivity() {
ActivityScenario<LifecycleOwnerActivity> activityScenario =
ActivityScenario.launch(LifecycleOwnerActivity.class);
assertThat(activityScenario).isNotNull();
activityScenario.onActivity(
activity -> {
assertThat(activity.getLifecycle().getCurrentState()).isEqualTo(State.RESUMED);
});
activityScenario.moveToState(State.STARTED);
activityScenario.onActivity(
activity -> {
assertThat(activity.getLifecycle().getCurrentState()).isEqualTo(State.STARTED);
});
activityScenario.moveToState(State.CREATED);
activityScenario.onActivity(
activity -> {
assertThat(activity.getLifecycle().getCurrentState()).isEqualTo(State.CREATED);
});
}
代码示例来源:origin: skydoves/ColorPickerView
/**
* removes this color picker observer from the the {@link LifecycleOwner}.
*
* @param lifecycleOwner {@link LifecycleOwner}.
*/
public void removeLifecycleOwner(LifecycleOwner lifecycleOwner) {
lifecycleOwner.getLifecycle().removeObserver(this);
}
代码示例来源:origin: skydoves/ColorPickerView
/**
* sets the {@link LifecycleOwner}.
*
* @param lifecycleOwner {@link LifecycleOwner}.
*/
public void setLifecycleOwner(LifecycleOwner lifecycleOwner) {
lifecycleOwner.getLifecycle().addObserver(this);
}
代码示例来源:origin: zhiwei1990/android-jetpack-demo
public void enable() {
enable = true;
if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
//判断,如果没有开始定位,就再次开启定位,定位成功,使用callback回调UI刷新
callBack.onSuccess();
}
}
代码示例来源:origin: googlecodelabs/android-lifecycles
public BoundLocationListener(LifecycleOwner lifecycleOwner,
LocationListener listener, Context context) {
mContext = context;
mListener = listener;
lifecycleOwner.getLifecycle().addObserver(this);
}
代码示例来源:origin: hazems/mvvm-sample-app
@Override
public void onClick(Project project) {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
((MainActivity) getActivity()).show(project);
}
}
};
代码示例来源:origin: skydoves/PowerMenu
/**
* sets {@link LifecycleOwner} for preventing memory leak.
*
* <p>if sets the {@link LifecycleOwner} this popup will be dismissed automatically
*
* <p>when onDestroy method called by lifecycle.
*
* @param lifecycleOwner {@link androidx.appcompat.app.AppCompatActivity},
* <p>{@link androidx.fragment.app.FragmentActivity} or etc are implements {@link
* LifecycleOwner}.
*/
public void setLifecycleOwner(LifecycleOwner lifecycleOwner) {
lifecycleOwner.getLifecycle().addObserver(this);
this.lifecycleOwner = lifecycleOwner;
}
代码示例来源:origin: PureWriter/about-page
public static void attach(@NonNull AbsAboutActivity activity, int index, boolean showDefaultCategory, @NonNull JsonConverter jsonConverter) {
RecommendedLoaderDelegate delegate = new RecommendedLoaderDelegate(activity, index, showDefaultCategory, jsonConverter);
activity.getLifecycle().addObserver(delegate);
delegate.start();
}
代码示例来源:origin: zhihu/SugarAdapter
SugarAdapter adapter = SugarAdapter.Builder.with(list)
.add(FooHolder.class)
.add(BarHolder.class, holder -> holder.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onSugarHolderCreated(@NonNull LifecycleOwner owner) {
内容来源于网络,如有侵权,请联系作者删除!