• Evenbus简单用法


    Evenbus是一个开源插件,可以帮我们在app里面进行数据传递,传递的对象为Object,就是说可以传输任何对象,但是一般为了拓展性和维护性,我们都用来传输Bean类型。

    这个插件最重要的是注册和反注册,因为只是注册了而不反注册,很容易引起内存泄漏,所以在不用的时候,必须把它反注册掉,值得一提的是注册和反注册必须成对出现在一个activity或者fragment里面,注意生命周期的配对使用,例如activity的话,

    onCreate对应onDestroy,onStart对应onStop
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            EventBus.getDefault().register(this);//注册
          
        }
    
      @Override
        protected void onDestroy() {
            super.onDestroy();
            EventBus.getDefault().unregister(this);//反注册
        }

    下面说说用法

    1.引入依赖,在app的build。gradle里面添加  compile 'org.greenrobot:eventbus:3.1.1'

    2.我们以传输Bean对象为例子讲解,所以要定义一个Bean对象

    public class TestBean {
        private String s;
    
        public String getS() {
            return s;
        }
    
        public void setS(String s) {
            this.s = s;
        }
    }

    3.在需要接收对象的页面进行注册和反注册

    public class MainActivity extends AppCompatActivity {
        private Button bt;
        private TextView tv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            EventBus.getDefault().register(this);
            bt = (Button) findViewById(R.id.bt);
            tv = (TextView) findViewById(R.id.tv);
            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tu2ac();
                }
            });
        }
        private void tu2ac() {
            Intent intent = new Intent(this, Main2Activity.class);
            startActivity(intent);
        }
        @Override
        protected void onDestroy() {
            super.onDestroy();
            EventBus.getDefault().unregister(this);
        }
        @Subscribe(threadMode = ThreadMode.MAIN)//在一般情况下,evenbus都是在主线程进行传输的,所以在传输过程中不要做耗时操作,可以在do something里面用异步线程做想做的事
        public void onMessageEvent(TestBean testBean) {
    //do something tv.setText(testBean.getS()); } }

    4.在发送对象的页面

    public class Main2Activity extends AppCompatActivity {
        private Button bt02;
        private TestBean mTestBean = new TestBean();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            mTestBean.setS("2222");
            bt02 = (Button) findViewById(R.id.bt02);
            bt02.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(mTestBean);//发送就直接发送,没有需要注意的事项,注意一处发送可以多处接受
                }
            });
        }
    }

    5.下面再贴上布局

    //这个是接受收对象的页面布局
    <?
    xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.moumou.evenbustest.MainActivity"> <Button android:id="@+id/bt" android:text="过去" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>


    //这个是发送对象的页面布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.moumou.evenbustest.Main2Activity">
    <Button
    android:id="@+id/bt02"
    android:text="发送"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    </LinearLayout>



  • 相关阅读:
    spingboot集成jpa(二)
    datanode与namenode的通信
    Yarn NodeManager restart
    hadoop上线和下线节点
    Spark读取文件
    eclipse安装中文补丁包
    eclipse中maven打包
    (转) eclipse安装lombok
    Windows 访问 Oracle
    wCF 问题收集页
  • 原文地址:https://www.cnblogs.com/laoyimou/p/7850102.html
Copyright © 2020-2023  润新知