新聞中心
EventBus is a publish/subscribe event bus optimized for Android.
青縣網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)從2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
so make it simple,just think EventBus as a framework that allow different compoents to communicate,
usually a subscribe register a certain event,then whenever a publisher has post an event,any subscribes who had register this event will be able to receive the notify.
This is an example,Let's say MainActivity is a subscribe who has register to receive RecvEvent event,
And Second Activity is a publicer who will post a RecvEvent when his btn is clicked.Theoretically speaking,MainActivity is able to recevie the event that posted by SecondActivity.
//From above,we know that EventBus contains three objects:Event,Subscribe,Publisher
//Event public class RecvEvent { private final String info; public RecvEvent(String info) { this.info = info; } public String getInfo() { return info; } } //Subscribe:MainActivity public class MainActivity extends AppCompatActivity { private TextView tvShowInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvShowInfo = (TextView)findViewById(R.id.tvShowRecv); //register to recevie ShowRecvEvent event EventBus.getDefault().register(this); } @Override protected void onDestroy() { super.onDestroy(); //destroy res EventBus.getDefault().unregister(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void onRecvEvent(ShowRecvEvent event) { tvShowInfo.setText(event.getInfo()); } public void onJump(View view) { startActivity(new Intent(this,SecondActivity.class)); } } //publisher:SecondActivity public class SecondActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); } public void onSend(View view) { EventBus.getDefault().post(new ShowRecvEvent("this is from second activity")); } }
新聞名稱:EventBusUsage
本文網(wǎng)址:http://www.dlmjj.cn/article/ipeisj.html