EventBus 浣跨敤

EventBus 鏄竴娆鹃拡瀵� Android 绔紭鍖栫殑 publish/subscribe 娑堟伅鎬荤嚎銆�

EventBus 浣跨敤_第1张图片
eventbus arch

涓昏鐩殑鏄浛浠ntent, Handler,BroadCast 鍦� Fragment锛孉ctivity锛孲ervice锛岀嚎绋嬩箣闂翠紶閫掓秷鎭紝浼樼偣鏄紑閿�灏忥紝浠g爜浼橀泤銆傚皢鍙戦�佽�呭拰鎺ユ敹鑰呰В鑰︺��

鍩烘湰鐢ㄦ硶

寮曞叆 eventbus:

compile 'de.greenrobot:eventbus:2.4.0'

瀹氫箟浜嬩欢:

public class MessageEvent { /* Additional fields if needed */ }

娉ㄥ唽浜嬩欢鎺ユ敹鑰咃細

eventBus.register(this);

鍙戦�佷簨浠�:

eventBus.post(event)

鎺ユ敹娑堟伅骞跺鐞�:

public void onEvent(MessageEvent event) {} 

娉ㄩ攢浜嬩欢鎺ユ敹锛�

eventBus.unregister(this);

鏈�鍚庯紝proguard 闇�瑕佸仛涓�浜涢澶栧鐞�:

# EventBus
-keepclassmembers class ** {
    public void onEvent*(**);
    void onEvent*(**);
}

鍩烘湰姒傚康

浣滀负涓�涓秷鎭�荤嚎锛屾湁涓変釜涓昏鐨勫厓绱狅細

  • Event锛氫簨浠躲�傚彲浠ユ槸浠绘剰绫诲瀷鐨勫璞�
  • Subscriber锛氫簨浠惰闃呰�咃紝鎺ユ敹鐗瑰畾鐨勪簨浠躲�傚湪EventBus涓紝浣跨敤绾﹀畾鏉ユ寚瀹氫簨浠惰闃呰�呬互绠�鍖栦娇鐢ㄣ�傚嵆鎵�鏈変簨浠惰闃呴兘閮芥槸浠nEvent寮�澶寸殑鍑芥暟锛屽叿浣撴潵璇达紝鍑芥暟鐨勫悕瀛楁槸onEvent锛宱nEventMainThread锛宱nEventBackgroundThread锛宱nEventAsync杩欏洓涓紝杩欎釜鍜孴hreadMode鏈夊叧
  • Publisher锛氫簨浠跺彂甯冭�咃紝鐢ㄤ簬閫氱煡 Subscriber 鏈変簨浠跺彂鐢熴�傚彲浠ュ湪浠绘剰绾跨▼浠绘剰浣嶇疆鍙戦�佷簨浠讹紝鐩存帴璋冪敤 eventBus.post(Object) 鏂规硶锛屽彲浠ヨ嚜宸卞疄渚嬪寲 EventBus 瀵硅薄锛屼絾涓�鑸娇鐢ㄩ粯璁ょ殑鍗曚緥灏卞ソ浜嗭細EventBus.getDefault()锛� 鏍规嵁 post 鍑芥暟鍙傛暟鐨勭被鍨嬶紝浼氳嚜鍔ㄨ皟鐢ㄨ闃呯浉搴旂被鍨嬩簨浠剁殑鍑芥暟銆�

鎸囧畾澶勭悊绾跨▼

榛樿鎯呭喌涓嬶紝onEvent 鏂规硶浼氬湪 post(Event) 鐨勬柟娉曠殑绾跨▼涓墽琛岋紝浣嗘槸鍙兘浼� post(Event) 鏄竴涓潪UI绾跨▼锛岃�� onEvent 鏂规硶闇�瑕佹洿鏂癠I锛岄渶瑕佸湪涓荤嚎绋嬭繍琛屻��

public void onEventMainThread(MessageEvent event) {
    textField.setText(event.message);
}

鍥犱负姣忎釜浜嬩欢璁㈤槄鍑芥暟閮芥槸鍜屼竴涓� ThreadMode 鐩稿叧鑱旂殑锛孴hreadMode 鎸囧畾浜嗕細璋冪敤鐨勫嚱鏁般�傛湁浠ヤ笅鍥涗釜 ThreadMode锛�

  • PostThread锛氫簨浠剁殑澶勭悊鍦ㄥ拰浜嬩欢鐨勫彂閫佸湪鐩稿悓鐨勮繘绋嬶紝鎵�浠ヤ簨浠跺鐞嗘椂闂翠笉搴斿お闀匡紝涓嶇劧褰卞搷浜嬩欢鐨勫彂閫佺嚎绋嬶紝鑰岃繖涓嚎绋嬪彲鑳芥槸UI绾跨▼銆傚搴旂殑鍑芥暟鍚嶆槸 onEvent銆�
  • MainThread: 浜嬩欢鐨勫鐞嗕細鍦║I绾跨▼涓墽琛屻�備簨浠跺鐞嗘椂闂翠笉鑳藉お闀匡紝杩欎釜涓嶇敤璇寸殑锛岄暱浜嗕細ANR鐨勶紝瀵瑰簲鐨勫嚱鏁板悕鏄痮nEventMainThread銆�
  • BackgroundThread锛氫簨浠剁殑澶勭悊浼氬湪涓�涓悗鍙扮嚎绋嬩腑鎵ц锛屽搴旂殑鍑芥暟鍚嶆槸 onEventBackgroundThread锛岃櫧鐒跺悕瀛楁槸 BackgroundThread锛屼簨浠跺鐞嗘槸鍦ㄥ悗鍙扮嚎绋嬶紝浣嗕簨浠跺鐞嗘椂闂磋繕鏄笉搴旇澶暱锛屽洜涓哄鏋滃彂閫佷簨浠剁殑绾跨▼鏄悗鍙扮嚎绋嬶紝浼氱洿鎺ユ墽琛屼簨浠讹紝濡傛灉褰撳墠绾跨▼鏄� UI 绾跨▼锛屼簨浠朵細琚姞鍒颁竴涓槦鍒椾腑锛岀敱涓�涓嚎绋嬩緷娆″鐞嗚繖浜涗簨浠讹紝濡傛灉鏌愪釜浜嬩欢澶勭悊鏃堕棿澶暱锛屼細闃诲鍚庨潰鐨勪簨浠剁殑娲惧彂鎴栧鐞嗐��
  • Async锛氫簨浠跺鐞嗕細鍦ㄥ崟鐙殑绾跨▼涓墽琛岋紝涓昏鐢ㄤ簬鍦ㄥ悗鍙扮嚎绋嬩腑鎵ц鑰楁椂鎿嶄綔锛屾瘡涓簨浠朵細寮�鍚竴涓嚎绋嬶紙鏈夌嚎绋嬫睜锛夛紝浣嗘渶濂介檺鍒剁嚎绋嬬殑鏁扮洰銆�

浣跨敤 Sticky Events

鏌愪簺鏃跺�欙紝鎴戜滑骞朵笉甯屾湜鍙戝竷鍑烘潵鐨凟vent绔嬪嵆琚秷璐规帀锛岃�屾槸绛夊埌鏃舵満鎴愮啛銆傛瘮濡傝锛屽湪涓�涓鎯呴〉鐐硅禐涔嬪悗锛屼骇鐢熶竴涓猇oteEvent锛孷oteEvent骞朵笉绔嬪嵆琚秷璐癸紝鑰屾槸绛夌敤鎴烽��鍑鸿鎯呴〉鍥炲埌鍟嗗搧鍒楄〃涔嬪悗锛屾帴鏀跺埌璇ヤ簨浠讹紝鐒跺悗鍒锋柊Adapter绛夈�傚叾瀹炶繖灏辨槸涔嬪墠鎴戜滑鐢╯tartActivityForResult鍜宱nActivityResult鍋氱殑浜嬫儏銆�

閭d箞鎬庝箞鐢‥ventBus鏉ユā鎷熻繖鏍风殑鎯呭喌鍛紵杩欏氨闇�瑕佸彟澶栦竴涓己澶х殑鏈哄埗锛孲ticky Event.

鍜屾櫘閫氱殑Event涓嶅悓锛屽綋鍙戝竷涓�涓猄ticky Event锛岄渶瑕佷娇鐢╬ostSticky鏂规硶锛�

EventBus.getDefault().postSticky(new VoteEvent(obj));

涓婇潰鐨勪唬鐮佸彂甯冧簡涓�涓偣璧炵殑Sticky Event.

...
EventBus.getDefault().registerSticky(this);
...

public void onEventMainThread(VoteEvent event) {
    // TODO update adapter
}

EventBus 鐨� registerSticky 鏂规硶鍦ㄦ瘡娆℃墽琛岀殑鏃跺�欙紝閮戒細鍘绘煡璇竴娆� Sticky Event锛屽苟璋冪敤鍝嶅簲鐨勬柟娉曞鐞嗐��

闄や簡 registerSticky 鏂规硶鏉ヨЕ鍙慡ticky Event 涔嬪锛屾垜浠繕鍙互閫氳繃 getStickyEvent 鏂规硶鏉ヨ幏鍙栧搷搴旂殑 Sticky Event 鏉ヨ繘琛屽鐞嗐�傛瘮濡�,鎴戜滑鍙互鍦� onResume 鏂规硶閲岄潰妫�鏌� EventBus 閲岄潰鏄惁鏈� VoteEvent 鐨凷ticky Event锛屼粠鑰岃繘琛屽鐞嗭細

@Override
public void onResume() {
    super.onResume();
    VoteEvent voteEvent = EventBus.getDefault().getStickyEvent(VoteEvent.class);
}

3.0 beta 鐗堟湰鐨勫彉鍖�

3.0 鐗堟湰鐩墠杩樺湪 beta 闃舵锛屼富瑕佸彉鍖栨椂浣跨敤娉ㄨВ浠f浛鍩轰簬鎯緥鐨� onEvent 鏂规硶锛岀敤娉曞涓嬶細

@Subscribe(threadMode = ThreadMode.MainThread)
public void onVoteEvent(VoteEvent event) {
}

杩涢樁

鎯宠鎺㈢储鏇村菇娣辩殑涓栫晫锛屽氨鍘荤湅婧愮爜鍚э細

EventBus婧愮爜鐮旇

你可能感兴趣的:(EventBus 浣跨敤)