我需要在LifecycleObserver中获得LifecycleOwner,以将其传递给ViewModel观察器。
这是我的MainActivity,如果我添加了LifecycleObserver。
public class MainActivity extends AppCompatActivity implements LifecycleOwner{
private LifecycleRegistry mLifecycleRegistry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow();
}
mLifecycleRegistry=new LifecycleRegistry(this);
mLifecycleRegistry.markState(Lifecycle.State.CREATED);
getLifecycle().addObserver(new MyLifecycleObserver());
}
@NonNull
@Override
public Lifecycle getLifecycle() {
return mLifecycleRegistry;
}
}
这是我的观察者,我需要LifecycleOwner。
public class MyLifecycleObserver implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStartListener(){
FirebaseMassage.startFirebase();
MainFragment.massageViewModel.getMassage().observe(/*here I need the LifecycleOwner*/, textMassage -> {
FirebaseMassage.updateFirebaseMassage(textMassage);
});
}
}
答案 0 :(得分:2)
您可以使用另一个签名来获得LifecycleOwner
,例如:
public class MyLifecycleObserver implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStartListener(LifecycleOwner owner){
...
}
}
答案 1 :(得分:0)
您不需要实现自己的LifecycleRegistry-只需使用AppCompatActivity提供的一个即可
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow();
}
getLifecycle().addObserver(new MyLifecycleObserver());
}
}
如果将startFirebase调用和viewmodel观察器分开,则可以直接在片段中观察viewmodel的变化,即
MyLifecycleObserver在发出ON_START时启动firebase调用。
public class MyLifecycleObserver implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStartListener(){
FirebaseMassage.startFirebase();
}
}
MainFragment直接观察ViewModel。
public class MainFragment extends Fragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
massageViewModel.getMassage().observe(this, textMassage -> {
FirebaseMassage.updateFirebaseMassage(textMassage);
});
}
答案 2 :(得分:0)
观察者方法可以接收零或一个参数。如果使用了(意味着您也可以使用零参数,但使用 IFF 参数),则第一个参数必须为 LifecycleOwner 类型。带有 Lifecycle.Event.ON_ANY 注释的方法可以接收第二个参数,该参数的类型必须为 Lifecycle.Event 。
class TestObserver implements LifecycleObserver {
@OnLifecycleEvent(ON_CREATE)
void onCreated(LifecycleOwner source) {
//one argument possible
}
@OnLifecycleEvent(ON_START)
void onCreated() {
//no argument possible
}
@OnLifecycleEvent(ON_ANY)
void onAny(LifecycleOwner source, Event event) {
//two argument possible only for ON_ANY event
}
}
答案 3 :(得分:0)
Abhishek Kumar 回答的官方文档来自这里:
这是文档本身:
观察者方法可以接收零个或一个参数。如果使用,第一个参数必须是 LifecycleOwner
类型。
用 Lifecycle.Event.ON_ANY
注释的方法可以接收第二个参数,该参数必须是 Lifecycle.Event 类型。
class TestObserver implements LifecycleObserver {
@OnLifecycleEvent(ON_CREATE)
void onCreated(LifecycleOwner source) {}
@OnLifecycleEvent(ON_ANY)
void onAny(LifecycleOwner source, Event event) {}
}