使用事件总线eventbus替代startactivityforresult

    xiaoxiao2021-03-26  9

    前几天我介绍过带返回值的activity,其中有两种方法一种是startactivityforresult,另一种是接口回调,今天我介绍另一种方法是eventbus事件总线,eventbus是非常火的一种框架这个模式就是事件总线,它比观察者模式要先进很多。我借鉴了这位博主的思路实现替代forresult:http://blog.csdn.net/crazy__chen/article/details/47425779

    下面看实现思路首先自定义eventbus

    public class EventBus {     HashMap<Class<?>,ArrayList<Subscription>> subscriptionsByEventType =             new HashMap<Class<?>,ArrayList<Subscription>>();     MainThreadHandler mainThreadHandler = new MainThreadHandler(this, Looper.getMainLooper());     AsyThreadHandler asyThreadHandler = new AsyThreadHandler(this);     private final static EventBus instance = new EventBus();     public static EventBus getInstance(){            return instance;     }     private EventBus(){};     public void register(Object subscriber){         Class<?> clazz = subscriber.getClass();         Method[] methods = clazz.getMethods();         for(Method m:methods){             String name = m.getName();             if(name.startsWith("onEvent")){                 Class<?>[] params = m.getParameterTypes();                 ArrayList<Subscription> arr;                 if(subscriptionsByEventType.containsKey(params[0])){                     arr = subscriptionsByEventType.get(params[0]);                 }else{                     arr = new ArrayList<Subscription>();                 }                 int len = name.substring("onEvent".length()).length();                 Log.i("cky",len+"");                 Subscription sub;                 if(len==0){                     sub =  new Subscription(subscriber,new SubscriberMethod(m,params[0],0));                 }else{                     sub =  new Subscription(subscriber,new SubscriberMethod(m,params[0],1));                 }                 arr.add(sub);                 subscriptionsByEventType.put(params[0],arr);             }         }     }     public void post(Object event){         Class<?> clazz = event.getClass();         ArrayList<Subscription> arr = subscriptionsByEventType.get(clazz);         for(Subscription sub:arr){             if(sub.SubscriberMethod.type==0){                 asyThreadHandler.post(sub,event);             }else{                 mainThreadHandler.post(sub,event);             }         }     }     public void invoke(final Subscription sub, final Object event, Method m){         try {             m.invoke(sub.subscriber,event);         } catch (IllegalAccessException e) {             e.printStackTrace();         } catch (InvocationTargetException e) {             e.printStackTrace();         }     } }

    在activity里面注册事件总线 EventBus.getInstance().register(this);

    之后写事件总线的方法

        public void onEvent(String i){         Log.i("cky", i);     }     public void onEventMain(String i){         txt_display3.setText(i);     }     public void onEventMain(Info i){         txt_display4.setText(i.msg+i.str3+i.str2);     }

    进入第二个页面在其点击事件里面进行事件处理

                 String message = mMessage3.getText().toString(); //                EventBus.getDefault().post(message);                 EventBus.getInstance().post(message);                 finish();

    demo地址:https://github.com/yuanchongzhang/startactivity_CallBack_event_demo-master

    转载请注明原文地址: https://ju.6miu.com/read-500033.html

    最新回复(0)