An analysis of Android Otto Frame

Source: Internet
Author: User

Today is an introduction to a framework of Android's more Android event bus Eventbus mode that is used by Otto.

Otto Official website: http://square.github.io/otto/


I. Configuring Otto in Android Studio (direct download of the jar package import in eclipse)

Just like the other frameworks described earlier, it simply configures the following line of red fonts in Build.gradle to

dependencies {
Compile Filetree (dir: ' Libs ', include: [' *.jar '])
Compile ' com.android.support:appcompat-v7:19.+ '
//otto The packages you need to rely on
Compile ' com.squareup:otto:+ '

}


Ii. analysis of Otto's event bus frame

1. Why use the Otto framework?

The main function of the Otto framework is to help us reduce the coupling between multiple classes (decoupling).

For example: Between a Class A and another Class B, if a is to manipulate a method in B.


Traditional method: A directly invokes the method of the B-object (coupled together)

Event bus mechanism: A does not need to invoke the method of Class B, but only needs to produce and issue an "event notification" if B subscribes to the "event"

then it will accept the event and make the appropriate action. this will be decoupled.


2. Use of the Otto Framework (combined with code introduction)

The Otto framework is actually easier to understand than several previous Android open source frameworks.

It mainly applies to a class: Bus Class (for registering classes, unregistering classes, publishing events)

Two notes: @Subscribe (subscription) @Produce (production) "are annotations for the event"




For convenience, I do not use two classes to do this demo, but if you want to try it, but remember a little, whether it is

The Publisher class or the Subscriber class requires the bus to register the class and log off. Otherwise it will not be able to be recognized by the bus, so it will not be effective.

The following demo, just to let you know that "event" has been generated, post, all the classes subscribed to the event will receive the event, the order of acceptance, not controlled by us!

public class MyActivity extends actionbaractivity {@Override protected void onCreate (Bundle savedinstancestate) {        Super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_my); Findviewbyid (R.id.button_change). Setonclicklistener (New View.onclicklistener () {@Override public vo ID OnClick (view view) {busprovider.getbusinstance (). Post (new Datachangedevent ("This is changed String"));    /Publish Event}});        } @Override protected void Onresume () {super.onresume (); Busprovider.getbusinstance (). Register (this);//register} @Override protected void OnPause () {Super.onpa        Use (); Busprovider.getbusinstance (). Unregister (this);//Logoff} @Subscribe//subscription event datachangedevent public void Saygoodoneve    NT (Datachangedevent event) {LOG.E ("event", "good");  } @Subscribe//subscription event public void Saybadonevent (Datachangedevent event) {LOG.E ("event", "bad");  } @Produce//Generate event Public datachangedevent producedatachangedevent () {return new datachangedevent ("This I    S changed String "); }}

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.