您好,登录后才能下订单哦!
EventBus is a publish/subscribe event bus optimized for Android.
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")); } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。