事件总线

一、添加依赖

    compile 'org.greenrobot:eventbus:3.0.0'

二、添加布局文件

active_main.xml





    
    

activity_second.xml文件



    
    

三、程序

先定义一个事件类

package com.example.mpcc.listviewrefrsh;

public class MessageEvent {
    private String message;
    public MessageEvent(String message) {
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

MianActivity.java

package com.example.mpcc.listviewrefrsh;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;



public class MainActivity extends AppCompatActivity {
    private TextView tv_message;
    private Button bt_message;
    private Button bt_subscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ddd_layout);
        tv_message = (TextView)this.findViewById(R.id.tv_message);
        tv_message.setText("MainActivity");
        bt_message = (Button)this.findViewById(R.id.bt_message);
        bt_message.setText("跳转到second");
        bt_subscription = (Button)this.findViewById(R.id.bt_subscription);
        bt_subscription.setText("跳转到second");
        bt_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,SecondActivity.class));
            }
        });


        bt_subscription.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().register(MainActivity.this);
            }
        });

    }
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);;
    }
//@Subscribe(threadMode = ThreadMode.POSTING,sticky = true)

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent){
        tv_message.setText(messageEvent.getMessage());
    }
}

secondActivity.java

package com.example.mpcc.listviewrefrsh;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

public class SecondActivity extends AppCompatActivity {
private Button bt_message;
private TextView tv_message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        tv_message = (TextView)this.findViewById(R.id.tv_message);
        tv_message.setText("SecondActivity");
        bt_message = (Button)findViewById(R.id.bt_message);
        bt_message.setText("发送事件");
        bt_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEvent("dandnan"));
               // finish();
            }
        });

    }
}

 

你可能感兴趣的:(Android应用)