The following is purely a product of personal understanding. I hope you will accept what is right and ignore what is wrong.
EventBus began to emerge in about 2015, but it began to be widely used in 2016, and its internal annotation form was adopted. In the development aspect, it reduced the precipitation of Android code and made the code written by developers more concise; in the function aspect, it replaced part of intent, BroadcaseReceiver's function effect, and also realized cross-process communication; in the optimization aspect, it also adopted annotation form. It contains a variety of operating conditions and environments (which is not well understood)
EventBus is also relatively simple to use. Of course, after you have read this blog with your heart and soul, it's not as difficult as I thought before. In fact, after you have spent 20 minutes reading it line by line, you will find that it only takes a minute to complete it. The main thing is to remember to look at the thought, the usage is only the second.
Effect :
Use steps:
Since personal understanding has something in common with RxJava and RxAndroid, the package name comes with the Rx series. At the same time, you can learn from the following blog addresses.
build dependencies:
compile 'org.greenrobot:eventbus:3.0.0'
Create Bean:
Main function in data storage, I think you konw!
Subscriber (aka - registration):
//Generally registered in OnCreat of the life cycle
EventBus.getDefault().register(this);
Unsubscribe (aka - logout):
//Cancellation is generally done in onDestroy of the life cycle
EventBus.getDefault().unregister(this);
Events:
//In general, we customize our method for handling our own events, but we need to pay attention to the parameters, as shown in the code below.
//The @Subscribe, which is especially noticed here, can be learned by the RxAndroid link attached to it if you don't understand it.
@Subscribe
public void onEventMainThread(MyFirstEventBus messageEvent){
mContent.setText(messageEvent.getStory());
}
Note: Registration, logout and event handling are generally in one class
Sender:
//Here's where we're going to send past data.
EventBus.getDefault().post(new MyFirstEventBus(" to do everything"));
MyFirstEventBus (which we use to place data Bean containers):
package com.example.dow.eventbus_rx;
public class MyFirstEventBus {
public String story;
public MyFirstEventBus(String story){
this.story=story;
}
public String getStory() {
return story;
}
public void setStory(String story) {
this.story = story;
}
}
MainActivity :
package com.example.dow.eventbus_rx;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
public class MainActivity extends AppCompatActivity {
private TextView mContent;
private TextView mBtn;
private TextView mIntentBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//First case
mBtn = (TextView) findViewById(R.id.tv_btn);
mContent = (TextView) findViewById(R.id.tv_content);
//Second case
mIntentBtn = (TextView) findViewById(R.id.tv_btn1);
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new MyFirstEventBus("We need happy to do everything"));
}
});
mIntentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,SecoundActivity.class));
}
});
//Register EventBus
EventBus.getDefault().register(this);
}
/**
*Handling events
* */
@Subscribe
public void onEventMainThread(MyFirstEventBus messageEvent){
mContent.setText(messageEvent.getStory());
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
SecoundActivity :
package com.example.dow.eventbus_rx;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
public class SecoundActivity extends AppCompatActivity {
private TextView mBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secound);
mBtn = (TextView) findViewById(R.id.secound_content);
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new MyFirstEventBus(" to do everything"));
finish();
}
});
}
}
MainActivity Xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.dow.eventbus_rx.MainActivity">
<TextView
android:layout_width="match_parent"
android:padding="5dp"
android:gravity="center"
android:id="@+id/tv_btn"
android:layout_height="wrap_content"
android:text="Click Events" />
<TextView
android:layout_width="match_parent"
android:padding="5dp"
android:gravity="center"
android:id="@+id/tv_content"
android:layout_height="wrap_content"
android:text="EventBus Have to display content" />
<TextView
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:padding="5dp"
android:gravity="center"
android:id="@+id/tv_btn1"
android:layout_height="wrap_content"
android:text="Jump Secound Event" />
</LinearLayout>
SecoundActivity Xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:id="@+id/secound_content"
android:text="Let's use it happily once. EventBus"
android:textSize="20sp"
android:gravity="center"
android:layout_height="match_parent" />
</LinearLayout>