OTTO event bus (2) official demo for android open-source projects

Source: Internet
Author: User

Note that the compiled version is later than 2.3. The default 1.6 does not support the match_parent attribute, causing layout file errors. You also need to manually add the android-support-v4 and otto to your libs folder. Main Code logic: 1. Click the clear button on the home page to publish two events and pass the objects. 2. Then, LocationHistoryFragment receives and processes the event object. 1. BusProvider uses MyProvider when calling a globally unique Bus instance object. getBusInstance () 1/* 2 * Copyright (C) 2012 Square, Inc. 3*4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except T in compliance with the License. 6 * You may obtain a copy of the License at 7*8 * http://www.apache.org/licenses/LICENSE-2.0 9*10 * Unless required by applicable law or agreed to in Writing, software11 * distributed under the License is distributed on an "as is" BASIS, 12 * without warranties or conditions of any kind, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16 17 package com. squareup. otto. sample; 18 19 import com. squareup. otto. bus; 20 21/** 22 * Maintains a singleton instance for o Btaining the bus. ideally this wocould be replaced with a more efficient means23 * such as through injection directly into interested classes.24 */25 public final class BusProvider {26 private static final Bus = new BUS (); 27 28 public static Bus getInstance () {29 return BUS; 30} 31 32 private BusProvider () {33 // No instances.34} 35} 2, click the clear button on the main page of LocationActivity to publish two event objects: LocationClearEvent and L OcationChangedEvent copy code findViewById (R. id. clear_location ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// Tell everyone to clear their location history. // clear the location BusProvider. getInstance (). post (new LocationClearEvent (); // Post new location event for the default location. // reload the default location lastLatitude = DEFAULT_LAT; lastlong1_= DEFAULT_LON; BusProvider. getInstan Ce (). post (produceLocationEvent () ;}}); copy code 1 @ Override protected void onResume () {2 super. onResume (); 3 4 // Register ourselves so that we can provide the initial value. 5 BusProvider. getInstance (). register (this); 6} 7 8 @ Override protected void onPause () {9 super. onPause (); 10 11 // Always unregister when an object no longer shoshould be on the bus.12 BusProvider. getInstance (). unregister (this ); 13} 3. The two objects LocationChangedEvent and LocationClearEvent to be passed during event sending mentioned above can be set as needed. For the code, see demo 4. The event objects in LocationHistoryFragment must also be registered and unregistered. Sometimes we release events in the service, you do not need to register the replication code @ Subscribe public void onLocationChanged (LocationChangedEvent event) {locationEvents. add (0, event. toString (); if (adapter! = Null) {adapter. notifyDataSetChanged () ;}@ Subscribe public void onLocationCleared (LocationClearEvent event) {locationEvents. clear (); if (adapter! = Null) {adapter. notifyDataSetChanged ();}

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.