EventBus 简单使用

添加依赖

compile 'de.greenrobot:eventbus:3.0.0-beta1'

  先获取到信息  然后发送 再跳转页面

case R.id.btn:
    //发送信息 s
    String s = mEd.getText().toString();
    EventBus.getDefault().postSticky(s);
    //跳转
    Intent intent = new Intent(MainActivity.this, ShowActivity.class);
    startActivity(intent);
    break;

再第二个页面中

package com.wlb.mydemo1;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class ShowActivity extends AppCompatActivity {

    private TextView mSs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        initView();
        //注册
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MainThread, sticky = true)
    public void dream(String str) { 
          //吐司接收到的数据     
      mSs.setText(str);
Toast. makeText(ShowActivity. this, str , Toast. LENGTH_SHORT).show() ; } private void initView() { mSs = (TextView) findViewById(R.id. ss) ; }}

猜你喜欢

转载自blog.csdn.net/jonly_w/article/details/80272216