Android Event-driven programming-based on Eventbus (ii)

Source: Internet
Author: User

Android Event-driven programming (II)

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

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

This article 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 look at the real code and introduce the basic usage of eventbus.

First I'll refer to (intercepted from the Eventbus warehouse) and introduce the entities that play a central role in event-driven programming.

    • Event Bus Eventbus: A central communication channel that connects all other entities;

    • Event events: The actions that occur can be almost anything (app start, receive some data, user interaction, etc.);

    • Subscriber Subscriber: The Subscriber listens to the event bus, and when there is an event in the bus, the subscriber will be triggered;

    • Publisher Publisher: Sends events to the event bus;

There is a clear understanding of what you can do with your own hands, so let's look at a simple example:

    • An application with two fragments loaded;

    • The second fragment contains a textview that TextView will be refreshed when the button is clicked;

    • When the new fragment is displayed on the current screen, the Actionbar title bar will change;

Host activity

The host activity needs to register Eventbus in its oncreate function:

EventBus.getDefault().register(this);

Once registered, the host activity can read the data from the bus, and we also need to unregister the Eventbus in the activity's OnDestroy function:

EventBus.getDefault().unregister(this);

The activity captures two different events: one for updating actionbar and one for loading the first fragment. We will write two onevent functions to handle these two events:

publicvoidonEventevent) {    event.getFragment()).addToBackStack(null).commit();}publicvoidonEvent(UpdateActionBarTitleEvent e) {    getActionBar().setTitle(e.getTitle()); }
Event

Each event needs to be declared in the class, and the event can contain variables:

publicclassprivate Fragment fragment;  publicShowFragmentEvent(Fragment fragment) {    this.fragment = fragment;  }  publicgetFragment() {    return fragment;  }}
The Fragments

Next we're going to create the fragments. The first fragment contains a button to open the second fragment, and the second fragment contains a button that refreshes TextView when the button is clicked. Fragments also requires registration and anti-registration Eventbus, in order to get a concise structure, we will define a basefragment to encapsulate these common operations.

Now let's create a few more actions, and the first fragment will open the second fragment with the following function:

@OnClickpublicvoidfirstButtonClick() {    EventBus.getDefault().post(new ShowFragmentEvent(new SecondFragment())); }

Note that I used the annotations defined in Butterknife to produce simpler and cleaner code. If you haven't used it yet, you should start using it now.

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

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

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

publicvoidonEventevent) {    textView.setText(event.getTitle()); }

Our simple application has two fragments, with events to achieve communication between the two fragments, a fragment through the event to get updates. I've uploaded the code to github and you can check it out and see it.

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

Android Event-driven programming-based on Eventbus (ii)

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.