本文整理了Java中androidx.lifecycle.Lifecycle.addObserver()
方法的一些代码示例,展示了Lifecycle.addObserver()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Lifecycle.addObserver()
方法的具体详情如下:
包路径:androidx.lifecycle.Lifecycle
类名称:Lifecycle
方法名:addObserver
[英]Adds a LifecycleObserver that will be notified when the LifecycleOwner changes state.
The given observer will be brought to the current state of the LifecycleOwner. For example, if the LifecycleOwner is in State#STARTED state, the given observer will receive Event#ON_CREATE, Event#ON_START events.
[中]添加LifecycleOwner更改状态时将收到通知的LifecycleObserver。
给定的观察者将处于LifecycleOwner的当前状态。例如,如果LifecycleOwner处于“已启动”状态,则给定的观察者将接收事件“正在创建”,事件“正在启动”。
代码示例来源: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: 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: 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: skydoves/ColorPickerView
/**
* sets the {@link LifecycleOwner}.
*
* @param lifecycleOwner {@link LifecycleOwner}.
*/
public void setLifecycleOwner(LifecycleOwner lifecycleOwner) {
lifecycleOwner.getLifecycle().addObserver(this);
}
代码示例来源:origin: googlecodelabs/android-lifecycles
public BoundLocationListener(LifecycleOwner lifecycleOwner,
LocationListener listener, Context context) {
mContext = context;
mListener = listener;
lifecycleOwner.getLifecycle().addObserver(this);
}
代码示例来源: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) {
代码示例来源:origin: xcesco/kripton
return;
owner.getLifecycle().addObserver(wrapper);
内容来源于网络,如有侵权,请联系作者删除!