The Otto event bus for Android Open source project (ii) official demo commentary

Source: Internet
Author: User

Official demo See Https://github.com/square/otto

Note that the compilation version is more than 2.3, the default of 1.6 does not support the Match_parent property, resulting in a layout file error.

Also need to manually add android-support-v4 and Otto to your own Libs folder.

Main code logic:

1, on the home page, the Clear button, publish two events and pass the object.

2, and then Locationhistoryfragment receives the event object and processes it.

1,busprovider provides a globally unique bus instance object

Use Myprovider.getbusinstance () when calling

1 /*2 * Copyright (C) Square, Inc.3  *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * You are not a use this file except in compliance with the License.6 * Obtain a copy of the License at7  *8  * http://www.apache.org/licenses/LICENSE-2.09  *Ten * unless required by applicable or agreed to writing, software One * Distributed under the License is distributed on a "as is" BASIS, A * without warranties or CONDITIONS of any KIND, either express or implied. - * See the License for the specific language governing permissions and - * Limitations under the License. the  */ -  - Package com.squareup.otto.sample; -  + import Com.squareup.otto.Bus; -  + /** A * Maintains a singleton instance for obtaining the bus. Ideally this would is replaced with a more efficient means at * such as through injection directly into interested classes. -  */ -  PublicFinalclassBusprovider { -   Private StaticFinal Bus bus =NewBus (); -  -    Public StaticBus getinstance () { in     returnBUS; -   } to  +   PrivateBusprovider () { -     //No instances. the   } *}
Busprovider

2,locationactivity Main Page

Clicking the Clear button will release two event objects, Locationclearevent and Locationchangedevent

Findviewbyid (r.id.clear_location). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//Tell the everyone to clear their. //Clear LocationBusprovider.getinstance (). Post (Newlocationclearevent ()); //Post New Location event for the default location. //Reload the default locationLastlatitude =Default_lat; Lastlongitude=Default_lon;      Busprovider.getinstance (). Post (Producelocationevent ()); }    });
View Code

To use the event bus don't forget to register and unregister

1@Overrideprotected voidOnresume () {2 Super.onresume ();3 4     //Register ourselves so we can provide the initial value.5Busprovider.getinstance (). Register ( This);6   }7 8@Overrideprotected voidOnPause () {9 super.onpause ();Ten  One     //Always unregister if an object no longer should is on the bus. ABusprovider.getinstance (). Unregister ( This); -}
View Code

3, the event mentioned above sends the two objects to be passed when the Locationchangedevent object and Locationclearevent

You can set the object arbitrarily according to your preference. Code See Demo

Receive event object in 4,locationhistoryfragment

Registration and anti-registration are also required. Sometimes when we post events in the service, there is no need to register

@Subscribe Public voidOnlocationchanged (locationchangedeventEvent) {Locationevents.add (0,Event. toString ()); if(Adapter! =NULL) {adapter.notifydatasetchanged (); }} @Subscribe Public voidOnlocationcleared (locationcleareventEvent) {locationevents.clear (); if(Adapter! =NULL) {adapter.notifydatasetchanged (); }
View Code

Related Article

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.