提交 b161a46a 编写于 作者: J jessyan

control eventbus

上级 0d5b6d24
......@@ -37,6 +37,7 @@ public abstract class BaseActivity<P extends BasePresenter> extends RxAppCompatA
private static final String LAYOUT_LINEARLAYOUT = "LinearLayout";
private static final String LAYOUT_FRAMELAYOUT = "FrameLayout";
private static final String LAYOUT_RELATIVELAYOUT = "RelativeLayout";
public static final String IS_NOT_ADD_ACTIVITY_LIST = "is_add_activity_list";//是否加入到activity的list,管理
private Unbinder mUnbinder;
......@@ -78,11 +79,15 @@ public abstract class BaseActivity<P extends BasePresenter> extends RxAppCompatA
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mApplication = (BaseApplication) getApplication();
//如果intent包含了此字段,并且为true说明不加入到list
// 默认为false,如果不需要管理(比如不需要在退出所有activity(killAll)时,退出此activity就在intent加此字段为true)
boolean isNotAdd = getIntent().getBooleanExtra(IS_NOT_ADD_ACTIVITY_LIST, false);
synchronized (BaseActivity.class) {
mApplication.getActivityList().add(this);
if (!isNotAdd)
mApplication.getActivityList().add(this);
}
EventBus.getDefault().register(this);//注册到事件主线
if (useEventBus())//如果要使用eventbus请将此方法返回true
EventBus.getDefault().register(this);//注册到事件主线
setContentView(initView());
//绑定到butterknife
mUnbinder = ButterKnife.bind(this);
......@@ -117,9 +122,20 @@ public abstract class BaseActivity<P extends BasePresenter> extends RxAppCompatA
}
if (mPresenter != null) mPresenter.onDestroy();//释放资源
if (mUnbinder != Unbinder.EMPTY) mUnbinder.unbind();
EventBus.getDefault().unregister(this);
if (useEventBus())//如果要使用eventbus请将此方法返回true
EventBus.getDefault().unregister(this);
}
/**
* 是否使用eventBus,默认为使用(true),
*
* @return
*/
protected boolean useEventBus() {
return false;
}
@Override
public void onBackPressed() {
super.onBackPressed();
......
......@@ -40,7 +40,8 @@ public abstract class BaseFragment<P extends BasePresenter> extends RxFragment {
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mActivity = (BaseActivity) getActivity();
EventBus.getDefault().register(this);//注册到事件主线
if (useEventBus())//如果要使用eventbus请将此方法返回true
EventBus.getDefault().register(this);//注册到事件主线
ComponentInject();
initData();
}
......@@ -61,7 +62,17 @@ public abstract class BaseFragment<P extends BasePresenter> extends RxFragment {
public void onDestroy() {
super.onDestroy();
if (mPresenter != null) mPresenter.onDestroy();//释放资源
EventBus.getDefault().unregister(this);
if (useEventBus())//如果要使用eventbus请将此方法返回true
EventBus.getDefault().unregister(this);
}
/**
* 是否使用eventBus,默认为使用(true),
*
* @return
*/
protected boolean useEventBus() {
return false;
}
......@@ -70,10 +81,29 @@ public abstract class BaseFragment<P extends BasePresenter> extends RxFragment {
protected abstract void initData();
/**
* 此方法是让外部调用使fragment做一些操作的,比如说外部的activity想让fragment对象执行一些方法,
* 建议在有多个需要让外界调用的方法时,统一传bundle,里面存一个what字段,来区分不同的方法,在setData
* 方法中就可以switch做不同的操作,这样就可以用统一的入口方法做不同的事,和message同理
*
* 使用此方法时请注意调用时fragment的生命周期,如果调用此setData方法时onActivityCreated
* 还没执行,setData里调用presenter的方法时,是会报空的,因为dagger注入是在onActivityCreated
* 方法中执行的,如果要做一些初始化操作,可以不必让外部调setData,在内部onActivityCreated中
* 初始化就可以了
*
* @param data
*/
public void setData(Object data) {
}
/**
* 使用此方法时请注意调用时fragment的生命周期,如果调用此setData方法时onActivityCreated
* 还没执行,setData里调用presenter的方法时,是会报空的,因为dagger注入是在onActivityCreated
* 方法中执行的,如果要做一些初始化操作,可以不必让外部调setData,在内部onActivityCreated中
* 初始化就可以了
*
*/
public void setData() {
}
......
......@@ -6,8 +6,6 @@ import android.view.View;
import com.jess.arms.utils.KnifeUtil;
import com.zhy.autolayout.utils.AutoUtils;
import org.simple.eventbus.EventBus;
/**
* Created by jess on 2015/11/24.
*/
......@@ -19,7 +17,6 @@ public abstract class BaseHolder<T> extends RecyclerView.ViewHolder implements V
itemView.setOnClickListener(this);//点击事件
AutoUtils.autoSize(itemView);//适配
KnifeUtil.bindTarget(this, itemView);//绑定
EventBus.getDefault().register(this);//注册eventbus
}
......
......@@ -34,12 +34,14 @@ public class BasePresenter<M, V extends BaseView> implements presenter {
@Override
public void onStart() {
EventBus.getDefault().register(this);//注册eventbus
if (useEventBus())//如果要使用eventbus请将此方法返回true
EventBus.getDefault().register(this);//注册eventbus
}
@Override
public void onDestroy() {
EventBus.getDefault().unregister(this);//解除注册eventbus
if (useEventBus())//如果要使用eventbus请将此方法返回true
EventBus.getDefault().unregister(this);//解除注册eventbus
unSubscribe();//解除订阅
this.mModel = null;
this.mRootView = null;
......@@ -49,6 +51,15 @@ public class BasePresenter<M, V extends BaseView> implements presenter {
}
/**
* 是否使用eventBus,默认为使用(true),
*
* @return
*/
protected boolean useEventBus() {
return false;
}
protected void addSubscrebe(Subscription subscription) {
if (mCompositeSubscription == null) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册