When the Android project becomes larger and larger, the communication between the various components of the application becomes more and more complex, so we usually use the decoupled component EventBus in Android. EventBus is a publish/subscribe event bus optimized for Android. The main function is to replace Intent, Handler, and BroadCast to pass messages between Fragment, Activity, Service, and thread. The advantage is that the overhead is small and the code is more elegant. And decoupling sender and receiver.
EventBus documentation: http://greenrobot.org/eventbus/documentation
EventBus source code: https://github.com/greenrobot/EventBus.git
There are four components involved in the EventBus framework
The relationship between subscribers, publishers, subscription events, and event bus can be represented by the official diagram:
EventBus1.0
First you need to add dependencies to your app:
compile 'org.simple:androideventbus:1.0.5.1'
There are three parts used in Activity:
1. Registration (under the onCreate method)
EventBus.getDefault().register(this);
2. Monitor (you can customize the method)
@Subscriber(tag = Utils.RESULTDATA) public void resultData(String title) { username.setText(title); }
3. Unregister (under the onDestroy method)
EventBus.getDefault().unregister(this);
Then it is to publish the event to let the listening event receive the communication information. When updating the UI operation, the relevant code is as follows:
Activity
/**** * eventbus communication *Version 1.0 */ public class MainActivity extends AppCompatActivity implements View.OnClickListener{ EditText username; EditText password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); this.setTitle("Login interface"); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); initView(); } private void initView() { username=(EditText)findViewById(R.id.username); password=(EditText)findViewById(R.id.password); findViewById(R.id.login).setOnClickListener(this); findViewById(R.id.register).setOnClickListener(this); } /*** * (String)->It can be any type here--(It will correspond when returning) * @param title */ @Subscriber(tag = Utils.RESULTDATA) public void resultData(String title) { username.setText(title); } //quit @Subscriber(tag=Utils.EXIT) public void exit(boolean isFang) { if (isFang) { finish(); } } @Override public void onClick(View v) { String name=username.getText().toString(); String pwd=password.getText().toString(); switch (v.getId()) { case R.id.login: break; case R.id.register: startActivity(new Intent(MainActivity.this,RegisterActivity.class)); break; } } /*** * Unregister */ @Override protected void onDestroy() { super.onDestroy (); EventBus.getDefault().unregister(this); } }
Communication Activity
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{ EditText username; LinearLayout linearLayout; Button register; Button exit; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); this.setTitle("Registration interface"); EventBus.getDefault().register(this); initView(); } private void initView() { username=(EditText)findViewById(R.id.username); linearLayout=(LinearLayout)findViewById(R.id.linear); linearLayout.setVisibility(View.GONE); register=(Button) findViewById(R.id.login); register.setText("Register"); register.setOnClickListener(this); exit=(Button)findViewById(R.id.register); exit.setText("exit"); exit.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.login://register String string=username.getText().toString(); if (TextUtils.isEmpty(string)) { Toast.makeText(getApplicationContext(),"Username cannot be empty",Toast.LENGTH_LONG).show(); } else { // publish event EventBus.getDefault().post(string,Utils.RESULTDATA); finish(); } break; case R.id.register://exit EventBus.getDefault().post(true,Utils.EXIT); finish(); break; } } /*** * Unregister */ @Override protected void onDestroy() { super.onDestroy (); EventBus.getDefault().unregister(this); } }
The effect is as follows: (including user name and logout communication)
EventBus3.0
First you need to add dependencies to your app:
compile'org.greenrobot:eventbus:3.0.0'
Registration and unregistration are the same, the listening event has changed
@Subscribe(threadMode = ThreadMode.MAIN) public void onUserName(User user) { username.setText(user.getUsername()); }
There are four threadModes:
- NAIN UI main thread
- BACKGROUND Background thread
- POSTING and the publisher are in the same thread
- ASYNC asynchronous thread
You can also subscribe to the priority of the event, you need to add parameters, as follows:
/** * sticky。 * Mode 2 */ @Subscribe(sticky = true) public void onExit(User user) { username.setText(user.getUsername()); }
The effect of EventBus3.0 is as follows: (two communication methods including username)
Source code click to download