EventBus 鏄竴娆鹃拡瀵� Android 绔紭鍖栫殑 publish/subscribe 娑堟伅鎬荤嚎銆�
涓昏鐩殑鏄浛浠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婧愮爜鐮旇