1. 程式人生 > >EventBus - Android's Event Bus

EventBus - Android's Event Bus

word inpu ive show [1] -c tin iss cli

技術分享圖片

[1] About EventBus

EventBus is a publish/subscribe event bus for Android

技術分享圖片

EventBus...

  • simplifies the communication between components
    • decouples event senders and receivers
    • performs well with Activities, Fragments, and background threads
    • avoids complex and error-prone dependencies and life cycle issues
  • makes your code simpler
  • is fast
  • is tiny (~50k jar)
  • is proven in practice by apps with 100,000,000+ installs
  • has advanced features like delivery threads, subscriber priorities, etc.

[2] Add EventBus

In your build.gradle :

dependencies {
    compile ‘org.greenrobot:eventbus:3.0.0‘
}

[3] Use EventBus

  1. Define events:
package com.netcircle.myeventbusdemo;
/**
 * Created by sweetgirl on 2017/11/27
 */
public class UserMessage {
    public final String mName ;
    public final String mPassword;

    public UserMessage(String name, String password){
        this.mName = name;
        this.mPassword = password;
    }
}
  1. Prepare subscribers:
package com.netcircle.myeventbusdemo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private EditText input_name;
    private EditText input_password;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
        initView();

    }

    private void initView(){

        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tv_signup);
        input_name = (EditText) findViewById(R.id.input_name);
        input_password = (EditText) findViewById(R.id.input_password);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    @Subscribe
    public void onMessageEvent(UserMessage event) {

     Toast.makeText(MainActivity.this,"MainActivity"+event.mName+event.mPassword,Toast.LENGTH_SHORT).show();
     Log.i("MainActivity"+event.mName,"MainActivity-psw"+event.mPassword);

    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
  1. Post events:
package com.netcircle.myeventbusdemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.greenrobot.eventbus.EventBus;
public class RegisterActivity extends AppCompatActivity {

    private EditText et_input_name;
    private EditText et_input_password;
    private Button btn_registered;

    private String name;
    private String psw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


        et_input_name = (EditText) findViewById(R.id.et_input_name);
        et_input_password = (EditText) findViewById(R.id.et_input_password);
        btn_registered = (Button) findViewById(R.id.btn_registered);


        name = et_input_name.getText().toString();
        psw = et_input_password.getText().toString();

        btn_registered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                name = et_input_name.getText().toString();
                psw = et_input_password.getText().toString();

                Log.i("RegisterActivity-name"+name,"RegisterActivity-psw"+psw);

                EventBus.getDefault().post(new UserMessage(name,psw));

                Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

[4] Demo Complete

see Log

11-28 10:39:05.656 25999-25999/com.netcircle.myeventbusdemo I/RegisterActivity-name123: RegisterActivity-pswqwe
11-28 10:39:05.676 25999-25999/com.netcircle.myeventbusdemo I/MainActivity123: MainActivity-pswqwe

EventBus - Android's Event Bus