我正在尝试使用Mosby和EventBus开发应用程序。我想要的第一个事件是在用户登录后创建一个粘性事件,这样每个屏幕都可以随时访问登录信息。
根据Mosby邮件样本,我有一个像这样的BasePresenter:
public abstract class EventBusPresenter<V extends MvpView> extends MvpBasePresenter<V> {
private static final java.lang.String TAG = tag(EventBusPresenter.class);
@Inject
protected EventBus mEventBus;
LoginSuccessfulEvent userInfo;
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onLoginSuccessful(LoginSuccessfulEvent event) {
MCLog.i(TAG, "Received a login event!");
userInfo = event;
onLoginReceived(event);
}
protected abstract void onLoginReceived(LoginSuccessfulEvent e);
public LoginSuccessfulEvent getUserInfo(){
return userInfo;
}
@Override
public void attachView(V view) {
super.attachView(view);
mEventBus.register(this);
}
@Override
public void detachView(boolean retainInstance) {
super.detachView(retainInstance);
mEventBus.unregister(this);
}
}
当用户登录时,我使用以下代码:
public void doLogin(String username, String password) {
if (isViewAttached()) {
getView().showLoading();
}
cancelSubscription();
mSubscriber = new Subscriber<AuthenticationResponse>() {
@Override
public void onCompleted() {
if (isViewAttached()) {
getView().loginSuccessful();
}
}
@Override
public void onError(Throwable e) {
if (isViewAttached()) {
getView().showLoginError();
}
}
@Override
public void onNext(AuthenticationResponse authenticationResponse) {
User user = authenticationResponse.getUser();
MCLog.w(TAG, String.format("Login was successful with user: %s", user) );
mEventBus.postSticky(new LoginSuccessfulEvent(user, authenticationResponse.getToken()));
String token = authenticationResponse.getToken();
MCLog.i(TAG, String.format("Token obtained = %s", token));
mSharedPreferences.edit().putString(PreferenceKeys.TOKEN_KEY, token).apply();
}
};
我的想法是,对于每个屏幕,只要加载它就可以通过EventBus订阅检索UserInfo。
问题是 - 这个事件太儿子了。根据mosby自己的BaseFragment类,我这样做:
public abstract class BaseFragment<V extends MvpView, P extends MvpPresenter<V>> extends MvpFragment<V, P> {
private Unbinder mUnbinder;
@LayoutRes
protected abstract int getLayoutRes();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(getLayoutRes(), container, false);
}
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
inject();
super.onViewCreated(view, savedInstanceState);
mUnbinder = ButterKnife.bind(this, view);
}
@Override public void onDestroyView() {
super.onDestroyView();
mUnbinder.unbind();
}
/**
* Inject dependencies
*/
protected void inject() {
}
}
这意味着注入在创建视图之前到达,因此每当我尝试响应正在接收的LoginEvent时,需要更新的UI元素就不再存在。
我怎样才能做到这一点?这甚至是正确的方法吗?
答案 0 :(得分:0)
在mUnbinder = ButterKnife.bind(this, view);
中onCreateView()
或onViewCreated()
在mUnbinder = ButterKnife.bind(this, view);
来电super.onViewCreated()
之前做UserMananger
,然后再致电UserManager
。
顺便说一下。使用EventBus听起来很痛苦。为什么不简单地使用像$strToRemove= array("www","http","blabla");
$newStr = str_replace($strToRemove, "", $_POST['billing_myfield12']);
这样的类来获取当前经过身份验证的用户,如果用户未经过身份验证则为null。此外,由于您使用的是RxJava,因此在每个屏幕上将str_replace
包含在RxJava链/流中是有意义的。