An analysis of Android Otto Frame

Source: Internet
Author: User

Today is a framework for using a much more Android event bus Eventbus mode in Android, Otto.

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


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

As with the other frameworks described earlier, it is only necessary to simply configure 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 depends on the package
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 a Class B. Suppose 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 class B method, but only needs to produce and issue an "event notification", assuming B subscribes to the "event"

then it will accept the event and make the corresponding operation. this will be decoupled.


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

The Otto framework is actually relative to several previous Android open source frameworks. Easier to understand.

It mainly applies to a class: Bus Class (for the registration class, write-off class. Announcement event)

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




For convenience, I did not use two classes to do this demo. Just assume that everyone is going to try it, just remember a little, whether it's

Both the Publisher class and the Subscriber class are required to use bus to register the class and write 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 the "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"));    /announcement event});        } @Override protected void Onresume () {super.onresume (); Busprovider.getbusinstance (). Register (this);//registration} @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 "); }}

An analysis of Android Otto Frame

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.