-
4
-
5
-
注册:
EventBus.getDefault().register(CActivity.this);
-
1
-
2
-
解注册:
EventBus.getDefault().removeAllStickyEvents(); EventBus.getDefault().unregister(CActivity.class);
-
1
-
2
-
3
四、举个栗子
- 主线程发送事件:
- 自定义事件(类似定义JavaBean),包含用户的姓名和密码;
public class UserEvent { private String name; private String password; public UserEvent() { } public UserEvent(String name, String password) { this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserEvent{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
在onCreate方法中注册订阅者,在onDestroy中解注册。
public class MainActivity extends AppCompatActivity { @BindView(R.id.jump) Button mJump; @BindView(R.id.send) Button mSend; @BindView(R.id.tv_result) TextView mTvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); //注册订阅者 EventBus.getDefault().register(this); } @OnClick({R.id.jump, R.id.send}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.jump: startActivity(new Intent(MainActivity.this, SecActivity.class)); break; case R.id.send: break; } } //定义处理接收的方法 @Subscribe(threadMode = ThreadMode.MAIN) public void userEventBus(UserEvent userEvent){ mTvResult.setText(userEvent.toString()); } @Override protected void onDestroy() { super.onDestroy(); //注销注册 EventBus.getDefault().unregister(this); } }
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
在另一个activity中发送事件,让订阅者能够接收;
@OnClick({R.id.sendData, R.id.receive}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.sendData: //发送事件 EventBus.getDefault().post(new UserEvent("Mr.sorrow", "123456")); finish(); break; case R.id.receive: break; } }
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
实现结果:
- 发送粘性事件:
- MainActivity中发送粘性事件;
case R.id.send: EventBus.getDefault().postSticky(new MessageEvent("粘性事件", "urgent")); startActivity(new Intent(MainActivity.this, SecActivity.class)); break;
-
1
-
2
-
3
-
4
-
5
-
SecActivity中接受注册并处理;
public class SecActivity extends AppCompatActivity { @BindView(R.id.sendData) Button mSendData; @BindView(R.id.receive) Button mReceive; @BindView(R.id.tv_receive) TextView mTvReceive; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sec); ButterKnife.bind(this); } @OnClick({R.id.sendData, R.id.receive}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.sendData: //发送事件 EventBus.getDefault().post(new UserEvent("Mr.sorrow", "123456")); finish(); break; case R.id.receive: //要接收时开始注册 EventBus.getDefault().register(SecActivity.this); break; } } //处理事件逻辑 @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) public void receiveEventBus(MessageEvent messageEvent) { mTvReceive.setText(messageEvent.toString()); } @Override protected void onDestroy() { super.onDestroy(); //解注册 EventBus.getDefault().removeAllStickyEvents(); EventBus.getDefault().unregister(SecActivity.this); } }
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
最后
感谢您的阅读,在文末给大家准备一个福利。本人从事Android开发已经有十余年,算是一名资深的移动开发架构师了吧。根据我的观察发现,对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。
所以在此将我十年载,从萌新小白一步步成长为Android移动开发架构师的学习笔记,从Android四大组件到手写实现一个架构设计,我都有一一的对应笔记为你讲解。
当然我也为你们整理好了百度、阿里、腾讯、字节跳动等等互联网超级大厂的历年面试真题集锦。这也是我这些年来养成的习惯,一定要学会把好的东西,归纳整理,然后系统的消化吸收,这样才能极大的提高学习效率和成长进阶。碎片、零散化的东西,我觉得最没有价值的。就好比你给我一张扑克牌,我只会觉得它是一张废纸,但如果你给我一副扑克牌,它便有了它的价值。这和我们收集资料就要收集那些系统化的,是一个道理。
最后,赠与大家一句诗,共勉!
不驰于空想,不骛于虚声。不忘初心,方得始终。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
成体系的学习效果低效漫长且无助。
所以在此将我十年载,从萌新小白一步步成长为Android移动开发架构师的学习笔记,从Android四大组件到手写实现一个架构设计,我都有一一的对应笔记为你讲解。
当然我也为你们整理好了百度、阿里、腾讯、字节跳动等等互联网超级大厂的历年面试真题集锦。这也是我这些年来养成的习惯,一定要学会把好的东西,归纳整理,然后系统的消化吸收,这样才能极大的提高学习效率和成长进阶。碎片、零散化的东西,我觉得最没有价值的。就好比你给我一张扑克牌,我只会觉得它是一张废纸,但如果你给我一副扑克牌,它便有了它的价值。这和我们收集资料就要收集那些系统化的,是一个道理。
[外链图片转存中…(img-PkkWdrR9-1714554525653)]
最后,赠与大家一句诗,共勉!
不驰于空想,不骛于虚声。不忘初心,方得始终。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!