Android event-driven programming-Based on EventBus (2), androideventbus

Source: Internet
Author: User

Android event-driven programming-Based on EventBus (2), androideventbus
Android event-driven programming (2)

-- Welcome to reprint, please indicate the source of http://blog.csdn.net/asce1885, do not use for commercial purposes without my consent, thank you --

Link: https://medium.com/google-developer-experts/event-driven-programming-for-android-part-ii-b1e05698e440

Gitbooks link: http://asce1885.gitbooks.io/android-rd-senior-advanced/content/androidshi_jian_qu_dong_bian_cheng_ff08_er_ff09.html

In the previous article, we briefly introduced event-driven programming. Now let's take a look at the real code and introduce the basic usage of EventBus.

First, I will refer to the entity that plays a central role in event-driven programming (captured from the EventBus repository.

  • Event bus EventBus: connects the central communication channels of all other entities;

  • Event: the action that occurs. It can be almost anything (application startup, receiving some data, user interaction, etc );

  • Subscriber: The Subscriber listens to the event bus. When an event in the bus is circulating, the Subscriber is triggered;

  • Publisher: Send events to the event bus;

Only by practice can we have a clear understanding. Here is a simple example:

  • One application loaded with two fragments;

  • The second fragment contains a TextView. When a button is clicked, The TextView is refreshed;

  • When the new fragment is displayed on the current screen, the ActionBar title bar changes accordingly;

Host Activity

The host Activity needs to register EventBus in its onCreate function:

EventBus.getDefault().register(this);

After registration, the host Activity can read data from the bus. At the same time, we need to register EventBus in the onDestroy function of the Activity:

EventBus.getDefault().unregister(this);

Activity captures two different events: one for updating the ActionBar and the other for loading the first fragment. We will write two onEvent functions to process these two events:

public void onEvent(ShowFragmentEvent event) {    getFragmentManager().beginTransaction().replace(R.id.container, event.getFragment()).addToBackStack(null).commit();}public void onEvent(UpdateActionBarTitleEvent e) {    getActionBar().setTitle(e.getTitle()); }
Event

Each event needs to be declared in the class. The event can contain variables:

public final class ShowFragmentEvent { private Fragment fragment;  public ShowFragmentEvent(Fragment fragment) {    this.fragment = fragment;  }  public Fragment getFragment() {    return fragment;  }}
The Fragments

Next we will create fragments. The first fragment contains a button to open the second fragment, and the second fragment contains a button. When you click the button, the TextView is refreshed. Fragments also needs to register and unregister EventBus. To get a simple structure, we will define a BaseFragment to encapsulate these public operations.

Now let's create more actions. The first fragment will open the second fragment through the following function:

@OnClick(R.id.first_button) public void firstButtonClick() {    EventBus.getDefault().post(new ShowFragmentEvent(new SecondFragment())); }

Note that I have used the annotation defined in ButterKnife to generate simpler and cleaner code. If you have not used it, you should start using it now.

The second fragment button will send an event to the event bus to update TextView:

EventBus.getDefault().post(new UpdateTextEvent(getString(R.string.text_updated)));

The second fragment needs to listen to this event at the same time, so that when it receives this event, it can change the text display accordingly:

public void onEvent(UpdateTextEvent event) {    textView.setText(event.getTitle()); }

Our simple application has two fragments, which implement communication between two fragments through events, and one fragment obtains updates through events. I have uploaded the code to GitHub. You can check it out and take a look.

A key issue is how to gradually upgrade an event-driven architecture. In the next article, I will introduce a concise and understandable architecture to support event-driven programming in Android.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.