[英]Android Architecture component LiveData - how to bind broadcastReceivers to lifecycle
Using Android LiveData I'd like to be able to unregister and register many BroadcastReceiver
s in the onInactive()
and onActive()
call backs. 使用Android LiveData我希望能够在
onInactive()
和onActive()
onInactive()
取消注册并注册许多BroadcastReceiver
。 So I want to do something like this: 所以我想做这样的事情:
public class BroadcastRecieverLiveData extends LiveData<BroadCastReciever> {
private BroadcastReciever reciever;
private Context context;
public BroadcastRecieverLiveData(Context context) {
this.context = context;
}
@Override
protected void onActive() {
IntentFilter filter = new IntentFilter();
filter.addAction("SOME_ACTION");
filter.addAction("SOME_OTHER_ACTION");
reciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//do something based on the intent's action
}
};
registerReceiver(reciever, filter);
}
@Override
protected void onInactive() {
if (reciever != null) {
context.unregisterReceiver(reciever);
}
}
}
I was thinking this could be a design pattern to be clean up of code instead of relaying on onDestroy
. 我认为这可能是一种设计模式,可以清理代码而不是在
onDestroy
上转发。 What are your thoughts on using LiveData this way? 您对这种方式使用LiveData有何看法? There is an example of using it here
有使用它的一个例子在这里
I think for receivers, you should implement LifecycleObserver
. 我认为对于接收者,您应该实现
LifecycleObserver
。 As per LiveData
documentation from Google codelab , 按照
LiveData
从谷歌文档代码实验室 ,
Caution: Storing a reference to a Context or View in a ViewModel can result in memory leaks.
警告:在ViewModel中存储对Context或View的引用可能导致内存泄漏。 Avoid fields that reference instances of the Context or View classes.
避免使用引用Context或View类实例的字段。 The onCleared() method is useful to unsubscribe or clear references to other objects with a longer lifecycle, but not for clearing references to Context or View objects.
onCleared()方法对于取消订阅或清除对具有更长生命周期的其他对象的引用非常有用,但不能用于清除对Context或View对象的引用。
So, You should not do context intensive operation in LiveData. 因此,您不应该在LiveData中执行上下文密集型操作。
Instead, take an example of below implementation, 相反,举一个以下实现的例子,
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.OnLifecycleEvent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class ReceiverManager implements LifecycleObserver {
private final Context mContext;
private final MyBrodacastReceiver myBrodacastReceiver;
public ReceiverManager(LifecycleOwner lifecycleOwner,
Context context) {
mContext = context;
myBrodacastReceiver = new MyBrodacastReceiver();
lifecycleOwner.getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void registerYourReceiver() {
mContext.registerReceiver(myBrodacastReceiver, new IntentFilter());
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void unRegisterYourReceiver() {
mContext.unregisterReceiver(myBrodacastReceiver);
}
private static class MyBrodacastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
}
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.