An easy way for Android model and view decoupling (no write interface required)

Source: Internet
Author: User

I gave this pattern to me named Mve (Model,view,eventbus). Model and view we all know, here is focused on Eventbus.

The Eventbus is a publish/Subscribe event bus optimized for Android. The main function is to substitute intent,handler,broadcast for passing messages between Fragment,activity,service and threads. The advantage is that the cost is small and the code is more elegant. and decoupling the sender and receiver.

OnCreate inside Register EventBus:EventBus.getDefault (). Register (this);

OnDestroy inside anti-registration EventBus:EventBus.getDefault (). Unregister (this);

Write a method in the activity or your fragment after the registration is written onEvent (event event)//name must be Onevent,event is the entity class in which the data is stored, and the data is processed in this method

The data returned by the asynchronous network request is parsed and placed inside the event entity class, calling Eventbus.getdefault (). Post (event); The OnEvent method that you have registered is executed.

An activity if more than one network requests the processing of different data, it is still written onevent method is only the event entity class is not the same, Eventbus will automatically distinguish. such as OnEvent (MyEvent event); OnEvent (Otherevent event)

An easy way for Android model and view decoupling (no write interface required)

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.