我刚刚开始了一个新的示例项目来使用 Mosby 测试 Conductor。问题是,在我离开 FirstScreen 或旋转设备之后,在 Rx onNext 回调之后,isViewAttached()
总是返回 false,我不明白为什么。一些片段需要澄清:
public abstract class MvpLceRxPresenter<V extends MvpLceView<M>, M> extends MvpBasePresenter<V>
implements MvpPresenter<V> {
protected Subscriber<M> subscriber;
protected void unsubscribe() {
if (subscriber != null && !subscriber.isUnsubscribed()) {
subscriber.unsubscribe();
}
subscriber = null;
}
public void subscribe(@NonNull UseCase useCase, @Nullable Bundle bundle, final boolean pullToRefresh) {
if (isViewAttached()) {
getView().showLoading(pullToRefresh);
}
useCase.unsubscribe();
subscriber = new Subscriber<M>() {
private boolean ptr = pullToRefresh;
@Override
public void onCompleted() {
MvpLceRxPresenter.this.onCompleted();
}
@Override
public void onError(Throwable e) {
MvpLceRxPresenter.this.onError(e, ptr);
}
@Override
public void onNext(M m) {
MvpLceRxPresenter.this.onNext(m);
}
};
if (useCase instanceof DynamicUseCase) {
DynamicUseCase dynamicUseCase = (DynamicUseCase) useCase;
dynamicUseCase.execute(subscriber, bundle);
} else {
useCase.execute(subscriber);
}
}
public void subscribe(Observable<M> observable, final boolean pullToRefresh) {
if (isViewAttached()) {
getView().showLoading(pullToRefresh);
}
unsubscribe();
subscriber = new Subscriber<M>() {
private boolean ptr = pullToRefresh;
@Override
public void onCompleted() {
MvpLceRxPresenter.this.onCompleted();
}
@Override
public void onError(Throwable e) {
MvpLceRxPresenter.this.onError(e, ptr);
}
@Override
public void onNext(M m) {
MvpLceRxPresenter.this.onNext(m);
}
};
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
protected void onCompleted() {
if (isViewAttached())
getView().showContent();
unsubscribe();
}
protected void onError(Throwable e, boolean pullToRefresh) {
if (isViewAttached())
getView().showError(e, pullToRefresh);
unsubscribe();
}
protected void onNext(M data) {
if (isViewAttached())
getView().setData(data);
}
@Override
public void detachView(boolean retainInstance) {
super.detachView(retainInstance);
if (!retainInstance)
unsubscribe();
}
我的主持人:
@DaggerScope(FirstScreenComponent.class)
public class FirstScreenPresenter extends MvpLceRxPresenter<FirstView, List<Contact>> {
private final GetContacts getContacts;
@Inject
public FirstScreenPresenter(GetContacts getContacts) {
this.getContacts = getContacts;
}
public void fetchContacts(boolean mustHaveNumber) {
Bundle bundle = new Bundle(1);
bundle.putBoolean("mustHaveNumber", mustHaveNumber);
subscribe(this.getContacts, bundle, false);
}
}
以及 FirstScreen 的某些部分:
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
swapNumberOnlyText(menu.findItem(R.id.number_only));
}
private void swapNumberOnlyText(MenuItem item) {
if(mustHaveNumber)
item.setTitle("Todos contatos");
else
item.setTitle("Somente com número");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.number_only){
mustHaveNumber = !mustHaveNumber;
this.presenter.fetchContacts(mustHaveNumber);
swapNumberOnlyText(item);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public List<Contact> getData() {
return adapter.getContacts();
}
@Override
public void setData(List<Contact> data) {
adapter.setContacts(data);
}
@Override
protected String getErrorMessage(Throwable e, boolean pullToRefresh) {
return null;
}
@NonNull
@Override
public LceViewState<List<Contact>, FirstView> createViewState() {
return new RetainingLceViewState<>();
}
@NonNull
@Override
public FirstScreenPresenter createPresenter() {
return presenter;
}
@Override
public void loadData(boolean pullToRefresh) {
this.presenter.fetchContacts(mustHaveNumber);
}
困扰我的是,当第一次打开时,我可以点击菜单项,它就像一个魅力,但如果我离开或旋转,描述的问题就开始了。
任何帮助将不胜感激 !