├── .gitignore ├── EventBusSample ├── .gitignore ├── EventBusSample.iml ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── kevintan │ │ └── eventbussample │ │ ├── MainActivity.java │ │ ├── bus │ │ ├── MoveToFragmentEvent.java │ │ ├── NormalEvent.java │ │ ├── SimpleEvent.java │ │ ├── StickyEvent.java │ │ └── UpdateActionBarTitleEvent.java │ │ ├── data │ │ └── DataObject.java │ │ └── fragments │ │ ├── BaseFragment.java │ │ ├── NoStickyFragment.java │ │ ├── SecondFragment.java │ │ └── ThirdFragment.java │ └── res │ ├── drawable-hdpi │ └── ic_launcher.png │ ├── drawable-mdpi │ └── ic_launcher.png │ ├── drawable-xhdpi │ └── ic_launcher.png │ ├── drawable-xxhdpi │ └── ic_launcher.png │ ├── layout │ ├── activity_main.xml │ ├── fragment_main.xml │ ├── fragment_no_sticky.xml │ └── fragment_second.xml │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | .DS_Store 5 | /build 6 | /gen 7 | local.properties 8 | /.idea/*.* 9 | *.iml -------------------------------------------------------------------------------- /EventBusSample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /gen 3 | local.properties 4 | 5 | -------------------------------------------------------------------------------- /EventBusSample/EventBusSample.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 17 | 18 | 19 | 20 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /EventBusSample/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'com.android.tools.build:gradle:0.12.+' 7 | } 8 | } 9 | apply plugin: 'android' 10 | 11 | repositories { 12 | mavenCentral() 13 | } 14 | 15 | android { 16 | compileSdkVersion 20 17 | buildToolsVersion "20.0.0" 18 | 19 | defaultConfig { 20 | minSdkVersion 13 21 | targetSdkVersion 20 22 | } 23 | } 24 | 25 | dependencies { 26 | compile 'de.greenrobot:eventbus:2.1.0' 27 | } 28 | -------------------------------------------------------------------------------- /EventBusSample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /EventBusSample/src/main/java/com/kevintan/eventbussample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.kevintan.eventbussample; 2 | 3 | import com.kevintan.eventbussample.bus.MoveToFragmentEvent; 4 | import com.kevintan.eventbussample.bus.StickyEvent; 5 | import com.kevintan.eventbussample.bus.UpdateActionBarTitleEvent; 6 | import com.kevintan.eventbussample.data.DataObject; 7 | import com.kevintan.eventbussample.fragments.BaseFragment; 8 | import com.kevintan.eventbussample.fragments.NoStickyFragment; 9 | import com.kevintan.eventbussample.fragments.SecondFragment; 10 | import com.kevintan.eventbussample.fragments.ThirdFragment; 11 | 12 | import android.app.Activity; 13 | import android.os.Bundle; 14 | import android.view.LayoutInflater; 15 | import android.view.View; 16 | import android.view.ViewGroup; 17 | import de.greenrobot.event.EventBus; 18 | 19 | /** 20 | * Sample main {@link android.app.Activity}. 21 | *

22 | * Created by kevintanhongann. 23 | *

24 | * Update: 25 | *

26 | * 1. Added event-handler that updates title of {@link android.app.ActionBar}. 27 | *

28 | * 2. Added event-handler that controls showing {@link com.kevintan.eventbussample.fragments.SecondFragment}, {@link 29 | * com.kevintan.eventbussample.fragments.ThirdFragment}. 30 | *

31 | * 3. Remove {@link com.kevintan.eventbussample.bus.StickyEvent} in {@link MainActivity#onDestroy()}. 32 | * 33 | * @author Xinyue Zhao 34 | */ 35 | public class MainActivity extends Activity { 36 | private static final int LAYOUT = R.layout.activity_main; 37 | 38 | /** 39 | * Handler for {@link com.kevintan.eventbussample.bus.UpdateActionBarTitleEvent} 40 | * 41 | * @param e 42 | * Event {@link com.kevintan.eventbussample.bus.UpdateActionBarTitleEvent}. 43 | */ 44 | public void onEvent(UpdateActionBarTitleEvent e) { 45 | getActionBar().setTitle(e.getTitle()); 46 | } 47 | 48 | /** 49 | * Handler for {@link com.kevintan.eventbussample.bus.MoveToFragmentEvent} 50 | * 51 | * @param e 52 | * Event {@link com.kevintan.eventbussample.bus.MoveToFragmentEvent }. 53 | */ 54 | public void onEvent(MoveToFragmentEvent e) { 55 | if (e.getFragment() instanceof ThirdFragment) { 56 | getFragmentManager().beginTransaction().replace(R.id.container, e.getFragment()).addToBackStack(null) 57 | .commit(); 58 | } else if (e.getFragment() instanceof SecondFragment) { 59 | DataObject object = new DataObject("kevin tan", "kevintan@kevintan.com"); 60 | EventBus.getDefault().postSticky(new StickyEvent(object)); 61 | getFragmentManager().beginTransaction().replace(R.id.container, e.getFragment()).addToBackStack(null) 62 | .commit(); 63 | } else if (e.getFragment() instanceof NoStickyFragment) { 64 | DataObject object = new DataObject("Hello, world!", "www.github.com"); 65 | EventBus.getDefault().postSticky(new StickyEvent(object)); 66 | getFragmentManager().beginTransaction().replace(R.id.container, e.getFragment()).addToBackStack(null) 67 | .commit(); 68 | } 69 | } 70 | 71 | @Override 72 | protected void onCreate(Bundle savedInstanceState) { 73 | super.onCreate(savedInstanceState); 74 | setContentView(LAYOUT); 75 | EventBus.getDefault().register(this); 76 | if (savedInstanceState == null) { 77 | getFragmentManager().beginTransaction() 78 | .add(R.id.container, new PlaceholderFragment()) 79 | .commit(); 80 | } 81 | } 82 | 83 | @Override 84 | protected void onDestroy() { 85 | EventBus.getDefault().unregister(this); 86 | EventBus.getDefault().removeStickyEvent(StickyEvent.class); 87 | super.onDestroy(); 88 | } 89 | 90 | 91 | /** 92 | * A placeholder fragment containing a simple view. 93 | *

94 | * Update: 95 | *

96 | * Extends from {@link com.kevintan.eventbussample.fragments.BaseFragment} to demonstrate coexistence between {@link 97 | * de.greenrobot.event.EventBus#registerSticky(Object)} and {@link de.greenrobot.event.EventBus#register(Object)}. 98 | */ 99 | public static class PlaceholderFragment extends BaseFragment { 100 | private static final int LAYOUT = R.layout.fragment_main; 101 | 102 | public PlaceholderFragment() { 103 | } 104 | 105 | @Override 106 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 107 | Bundle savedInstanceState) { 108 | /* Update fragment's title.*/ 109 | EventBus.getDefault().post(new UpdateActionBarTitleEvent(getString(R.string.screen_1))); 110 | View rootView = inflater.inflate(LAYOUT, container, false); 111 | /* Move to fragment that can demonstrate sticky-event. */ 112 | rootView.findViewById(R.id.btn_test).setOnClickListener(new View.OnClickListener() { 113 | @Override 114 | public void onClick(View v) { 115 | EventBus.getDefault().post(new MoveToFragmentEvent(new SecondFragment())); 116 | } 117 | }); 118 | /* Move to fragment that can not accept sticky-event. */ 119 | rootView.findViewById(R.id.btn_no_sticky).setOnClickListener(new View.OnClickListener() { 120 | @Override 121 | public void onClick(View v) { 122 | EventBus.getDefault().post(new MoveToFragmentEvent(new NoStickyFragment())); 123 | } 124 | }); 125 | 126 | return rootView; 127 | } 128 | } 129 | 130 | } 131 | -------------------------------------------------------------------------------- /EventBusSample/src/main/java/com/kevintan/eventbussample/bus/MoveToFragmentEvent.java: -------------------------------------------------------------------------------- 1 | package com.kevintan.eventbussample.bus; 2 | 3 | import android.app.Fragment; 4 | 5 | /** 6 | * Event for moving to a fragment. 7 | * 8 | * @author Xinyue Zhao 9 | */ 10 | public final class MoveToFragmentEvent { 11 | /** 12 | * {@link android.app.Fragment} to switch. 13 | */ 14 | private Fragment mFragment; 15 | 16 | /** 17 | * Constructor of {@link MoveToFragmentEvent} 18 | * 19 | * @param _fragment 20 | * {@link android.app.Fragment} to switch. 21 | */ 22 | public MoveToFragmentEvent(Fragment _fragment) { 23 | mFragment = _fragment; 24 | } 25 | 26 | /** 27 | * Get the {@link android.app.Fragment} to switch. 28 | * 29 | * @return {@link android.app.Fragment} to switch. 30 | */ 31 | public Fragment getFragment() { 32 | return mFragment; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /EventBusSample/src/main/java/com/kevintan/eventbussample/bus/NormalEvent.java: -------------------------------------------------------------------------------- 1 | package com.kevintan.eventbussample.bus; 2 | 3 | /** 4 | * A simple normal event. 5 | */ 6 | public final class NormalEvent { 7 | } 8 | -------------------------------------------------------------------------------- /EventBusSample/src/main/java/com/kevintan/eventbussample/bus/SimpleEvent.java: -------------------------------------------------------------------------------- 1 | package com.kevintan.eventbussample.bus; 2 | 3 | 4 | /** 5 | * A simple event. 6 | */ 7 | public final class SimpleEvent { 8 | } 9 | -------------------------------------------------------------------------------- /EventBusSample/src/main/java/com/kevintan/eventbussample/bus/StickyEvent.java: -------------------------------------------------------------------------------- 1 | package com.kevintan.eventbussample.bus; 2 | 3 | import com.kevintan.eventbussample.data.DataObject; 4 | 5 | /** 6 | * Created by kevintanhongann on 11/27/13. 7 | *

8 | *

9 | * Update to move to package {@code com.kevintan.eventbussample.bus} and renamed. 10 | *

11 | * 12 | * @author Xinyue Zhao 13 | */ 14 | public class StickyEvent { 15 | private DataObject mDataObject; 16 | 17 | public StickyEvent(DataObject _dataObject) { 18 | mDataObject = _dataObject; 19 | } 20 | 21 | public DataObject getDataObject() { 22 | return mDataObject; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /EventBusSample/src/main/java/com/kevintan/eventbussample/bus/UpdateActionBarTitleEvent.java: -------------------------------------------------------------------------------- 1 | package com.kevintan.eventbussample.bus; 2 | 3 | /** 4 | * Update title of ActionBar. 5 | * 6 | * @author Xinyue Zhao 7 | */ 8 | public final class UpdateActionBarTitleEvent { 9 | /** 10 | * Title for {@link android.app.ActionBar}. 11 | */ 12 | private String mTitle; 13 | 14 | /** 15 | * Constructor of {@link UpdateActionBarTitleEvent}. 16 | * 17 | * @param _title 18 | * Title for {@link android.app.ActionBar}. 19 | */ 20 | public UpdateActionBarTitleEvent(String _title) { 21 | mTitle = _title; 22 | } 23 | 24 | /** 25 | * Get title for {@link android.app.ActionBar}. 26 | * 27 | * @return Title for {@link android.app.ActionBar}. 28 | */ 29 | public String getTitle() { 30 | return mTitle; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /EventBusSample/src/main/java/com/kevintan/eventbussample/data/DataObject.java: -------------------------------------------------------------------------------- 1 | package com.kevintan.eventbussample.data; 2 | 3 | /** 4 | * Created by kevintanhongann on 11/27/13. 5 | * 6 | *

7 | * Update to move to package {@code com.kevintan.eventbussample.bus} and renamed. 8 | *

9 | * @author Xinyue Zhao 10 | */ 11 | public class DataObject { 12 | 13 | private String name; 14 | 15 | private String email; 16 | 17 | public DataObject(String name, String email) { 18 | this.name = name; 19 | this.email = email; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public String getEmail() { 27 | return email; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return "TestObject{" + 33 | "name='" + name + '\'' + 34 | ", email='" + email + '\'' + 35 | '}'; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /EventBusSample/src/main/java/com/kevintan/eventbussample/fragments/BaseFragment.java: -------------------------------------------------------------------------------- 1 | package com.kevintan.eventbussample.fragments; 2 | 3 | import android.app.Fragment; 4 | import de.greenrobot.event.EventBus; 5 | 6 | /** 7 | * {@link BaseFragment} demonstrates a general pattern used in App. 8 | *

9 | * Generally only when the App has been in foreground evens could be handled, so that we connect bus in {@code 10 | * onResume()}, and disconnect in {@code onPause()}. 11 | *

12 | * {@link com.kevintan.eventbussample.fragments.BaseFragment#onEvent(Object)} to tricky {@link 13 | * de.greenrobot.event.EventBusException} for a least one subscribed method "onEvent". 14 | *

15 | * Added {@link com.kevintan.eventbussample.fragments.BaseFragment#isStickyAvailable()} for subclasses whether using 16 | * sticky-mode or normal. 17 | */ 18 | public abstract class BaseFragment extends Fragment { 19 | /** 20 | * Handler for {@link } 21 | * 22 | * @param e 23 | * Event {@link }. 24 | */ 25 | public void onEvent(Object e) { 26 | 27 | } 28 | 29 | @Override 30 | public void onResume() { 31 | if (isStickyAvailable()) { 32 | EventBus.getDefault().registerSticky(this); 33 | } else { 34 | EventBus.getDefault().register(this); 35 | } 36 | super.onResume(); 37 | } 38 | 39 | @Override 40 | public void onPause() { 41 | EventBus.getDefault().unregister(this); 42 | super.onPause(); 43 | } 44 | 45 | /** 46 | * Is the {@link android.app.Fragment} ready to subscribe a sticky-event or not. 47 | * 48 | * @return {@code true} if the {@link android.app.Fragment} available for sticky-events inc. normal events. 49 | *

50 | * Default is {@code false}. 51 | */ 52 | protected boolean isStickyAvailable() { 53 | return false; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /EventBusSample/src/main/java/com/kevintan/eventbussample/fragments/NoStickyFragment.java: -------------------------------------------------------------------------------- 1 | package com.kevintan.eventbussample.fragments; 2 | 3 | import android.os.Bundle; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.View.OnClickListener; 7 | import android.view.ViewGroup; 8 | import android.widget.TextView; 9 | import android.widget.Toast; 10 | 11 | import com.kevintan.eventbussample.R; 12 | import com.kevintan.eventbussample.bus.NormalEvent; 13 | import com.kevintan.eventbussample.bus.SimpleEvent; 14 | import com.kevintan.eventbussample.bus.StickyEvent; 15 | import com.kevintan.eventbussample.bus.UpdateActionBarTitleEvent; 16 | 17 | import de.greenrobot.event.EventBus; 18 | 19 | /** 20 | * {@link NoStickyFragment} can not accept a sticky-event, but normal event is no problem. 21 | *

22 | * UI can't refresh although it implements handler that handles the {@link com.kevintan.eventbussample.bus.StickyEvent}. 23 | */ 24 | public final class NoStickyFragment extends BaseFragment { 25 | private static final int LAYOUT = R.layout.fragment_no_sticky; 26 | 27 | /** 28 | * Handler for {@link com.kevintan.eventbussample.bus.StickyEvent} 29 | * 30 | * @param e 31 | * Event {@link com.kevintan.eventbussample.bus.StickyEvent}. 32 | */ 33 | public void onEvent(StickyEvent e) { 34 | TextView textView = (TextView) getView().findViewById(R.id.text_tv); 35 | textView.setText(e.getDataObject().toString()); 36 | } 37 | 38 | /** 39 | * Handler for {@link com.kevintan.eventbussample.bus.NormalEvent} 40 | * 41 | * @param e 42 | * Event {@link com.kevintan.eventbussample.bus.NormalEvent}. 43 | */ 44 | public void onEvent(NormalEvent e) { 45 | TextView textView = (TextView) getView().findViewById(R.id.text_tv); 46 | textView.setText("Got a normal event."); 47 | 48 | } 49 | 50 | /** 51 | * Handler for {@link com.kevintan.eventbussample.bus.SimpleEvent}. 52 | * 53 | * @param e 54 | * Event {@link com.kevintan.eventbussample.bus.SimpleEvent}. 55 | */ 56 | public void onSimpleEvent(SimpleEvent e) { 57 | Toast.makeText(getActivity(),"onSimpleEvent handling SimpleEvent", Toast.LENGTH_LONG).show(); 58 | } 59 | 60 | 61 | @Override 62 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 63 | Bundle savedInstanceState) { 64 | /*Update ActionBar's title.*/ 65 | EventBus.getDefault().post(new UpdateActionBarTitleEvent(getString(R.string.screen_4))); 66 | return inflater.inflate(LAYOUT, container, false); 67 | } 68 | 69 | @Override 70 | public void onViewCreated(View view, Bundle savedInstanceState) { 71 | super.onViewCreated(view, savedInstanceState); 72 | view.findViewById(R.id.normal_event_btn).setOnClickListener(new View.OnClickListener() { 73 | @Override 74 | public void onClick(View v) { 75 | /*Can send a normal event and handle it.*/ 76 | EventBus.getDefault().post(new NormalEvent()); 77 | } 78 | }); 79 | 80 | view.findViewById(R.id.simple_event_btn).setOnClickListener(new OnClickListener() { 81 | @Override 82 | public void onClick(View v) { 83 | EventBus.getDefault().post(new SimpleEvent()); 84 | } 85 | }); 86 | } 87 | 88 | @Override 89 | public void onResume() { 90 | super.onResume(); 91 | EventBus.getDefault().register(this, "onSimpleEvent"); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /EventBusSample/src/main/java/com/kevintan/eventbussample/fragments/SecondFragment.java: -------------------------------------------------------------------------------- 1 | package com.kevintan.eventbussample.fragments; 2 | 3 | import com.kevintan.eventbussample.R; 4 | import com.kevintan.eventbussample.bus.MoveToFragmentEvent; 5 | import com.kevintan.eventbussample.bus.NormalEvent; 6 | import com.kevintan.eventbussample.bus.StickyEvent; 7 | import com.kevintan.eventbussample.bus.UpdateActionBarTitleEvent; 8 | import com.kevintan.eventbussample.data.DataObject; 9 | 10 | import android.os.Bundle; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.Toast; 15 | import de.greenrobot.event.EventBus; 16 | 17 | /** 18 | * SecondFragment where user can update the {@link com.kevintan.eventbussample.bus.StickyEvent} or moving to {@link 19 | * com.kevintan.eventbussample.fragments.ThirdFragment} that enhances the original sample codes. 20 | *

21 | * Created by kevintanhongann on 11/27/13. 22 | *

23 | * Update: 24 | *

25 | * 1. Demonstrate that {@link de.greenrobot.event.EventBus#postSticky(Object)} works including with normal {@link 26 | * de.greenrobot.event.EventBus#post(Object)}. 27 | *

28 | * 2. Update {@link com.kevintan.eventbussample.bus.StickyEvent} and more to third {@link android.app.Fragment} that 29 | * shows new {@link com.kevintan.eventbussample.bus.StickyEvent}. 30 | *

31 | * 3. Post event to update {@link android.app.ActionBar}'s title. 32 | *

33 | * 4. Post {@link com.kevintan.eventbussample.bus.MoveToFragmentEvent} that will be handled by{@link 34 | * com.kevintan.eventbussample.MainActivity} to switch to {@link com.kevintan.eventbussample.fragments.ThirdFragment}. 35 | *

36 | * 5. Move calling {@link de.greenrobot.event.EventBus#removeStickyEvent(Class)} on {@link 37 | * com.kevintan.eventbussample.bus.StickyEvent} into {@link com.kevintan.eventbussample.MainActivity#onDestroy()}. 38 | *

39 | * 6. Extends from {@link com.kevintan.eventbussample.fragments.BaseFragment} to demonstrate coexistence of {@link 40 | * de.greenrobot.event.EventBus#registerSticky(Object)} and {@link de.greenrobot.event.EventBus#register(Object)}. 41 | * 42 | * @author Xinyue Zhao 43 | */ 44 | public class SecondFragment extends BaseFragment { 45 | private static final int LAYOUT = R.layout.fragment_second; 46 | 47 | /** 48 | * Handler for {@link StickyEvent} 49 | * 50 | * @param e 51 | * Event {@link StickyEvent}. 52 | */ 53 | public void onEvent(StickyEvent e) { 54 | Toast.makeText(getActivity(), String.format("Sticky handler: %s", e.getDataObject().toString()), Toast.LENGTH_SHORT).show(); 55 | } 56 | 57 | /** 58 | * Handler for {@link com.kevintan.eventbussample.bus.NormalEvent} 59 | * 60 | * @param e 61 | * Event {@link com.kevintan.eventbussample.bus.NormalEvent}. 62 | */ 63 | public void onEvent(NormalEvent e) { 64 | Toast.makeText(getActivity(), R.string.msg_normal_event, Toast.LENGTH_SHORT).show(); 65 | } 66 | 67 | 68 | 69 | @Override 70 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 71 | Bundle savedInstanceState) { 72 | /*Update ActionBar's title.*/ 73 | EventBus.getDefault().post(new UpdateActionBarTitleEvent(getString(R.string.screen_2))); 74 | return inflater.inflate(LAYOUT, container, false); 75 | } 76 | 77 | @Override 78 | public void onViewStateRestored(Bundle savedInstanceState) { 79 | super.onViewStateRestored(savedInstanceState); 80 | } 81 | 82 | @Override 83 | public void onViewCreated(View view, Bundle savedInstanceState) { 84 | super.onViewCreated(view, savedInstanceState); 85 | view.findViewById(R.id.normal_event_btn).setOnClickListener(new View.OnClickListener() { 86 | @Override 87 | public void onClick(View v) { 88 | EventBus.getDefault().post(new NormalEvent()); 89 | } 90 | }); 91 | 92 | view.findViewById(R.id.update_event_btn).setOnClickListener(new View.OnClickListener() { 93 | @Override 94 | public void onClick(View v) { 95 | /*Update the sticky, LOOK the postSticky() runs including post() that onEvent(StickyEvent e) will be called late.*/ 96 | DataObject to = new DataObject("XinyueZ", "dev.xinyue.zhao@gmail.com"); 97 | StickyEvent newEvent = new StickyEvent(to); 98 | EventBus.getDefault().postSticky(newEvent); 99 | Toast.makeText(getActivity(), "New Sticky is posted.", Toast.LENGTH_SHORT).show(); 100 | } 101 | }); 102 | 103 | view.findViewById(R.id.next_btn).setOnClickListener(new View.OnClickListener() { 104 | @Override 105 | public void onClick(View v) { 106 | /*Move to 3rd fragment.*/ 107 | EventBus.getDefault().post(new MoveToFragmentEvent(new ThirdFragment())); 108 | } 109 | }); 110 | } 111 | 112 | @Override 113 | protected boolean isStickyAvailable() { 114 | return true; 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /EventBusSample/src/main/java/com/kevintan/eventbussample/fragments/ThirdFragment.java: -------------------------------------------------------------------------------- 1 | package com.kevintan.eventbussample.fragments; 2 | 3 | import com.kevintan.eventbussample.R; 4 | import com.kevintan.eventbussample.bus.StickyEvent; 5 | import com.kevintan.eventbussample.bus.UpdateActionBarTitleEvent; 6 | 7 | import android.os.Bundle; 8 | import android.view.View; 9 | import android.widget.Button; 10 | import de.greenrobot.event.EventBus; 11 | 12 | /** 13 | * {@link android.app.Fragment} shows last update of {@link com.kevintan.eventbussample.bus.StickyEvent}. 14 | *

15 | * Also update title on {@link android.app.ActionBar}. 16 | *

17 | * Demonstrate override of {@link EventBus} which is very different from Otto 18 | * Bus. 19 | */ 20 | public final class ThirdFragment extends SecondFragment { 21 | /** 22 | * Overwritten version of {@link com.kevintan.eventbussample.fragments.SecondFragment#onEvent(com.kevintan.eventbussample.bus.StickyEvent)} 23 | * to demonstrate advantage of {@link de.greenrobot.event.EventBus} which is very different from Otto Bus. 25 | * 26 | * @param _stickyEvent 27 | * A {@link com.kevintan.eventbussample.bus.StickyEvent}. 28 | */ 29 | @Override 30 | public void onEvent(StickyEvent _stickyEvent) { 31 | Button updateBtn = (Button) getView().findViewById(R.id.update_event_btn); 32 | updateBtn.setText(String.format("Override onEvent\n%s", _stickyEvent.getDataObject().toString())); 33 | } 34 | 35 | @Override 36 | public void onViewCreated(View view, Bundle savedInstanceState) { 37 | super.onViewCreated(view, savedInstanceState); 38 | /*Update ActionBar's title.*/ 39 | EventBus.getDefault().post(new UpdateActionBarTitleEvent(getString(R.string.screen_3))); 40 | View v = view.findViewById(R.id.update_event_btn); 41 | v.setEnabled(false); 42 | view.findViewById(R.id.next_btn).setVisibility(View.GONE); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /EventBusSample/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevintanhongann/EventBusSample/27506d11a58beb7be171d1c5ebabeedeb42de023/EventBusSample/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /EventBusSample/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevintanhongann/EventBusSample/27506d11a58beb7be171d1c5ebabeedeb42de023/EventBusSample/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /EventBusSample/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevintanhongann/EventBusSample/27506d11a58beb7be171d1c5ebabeedeb42de023/EventBusSample/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /EventBusSample/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevintanhongann/EventBusSample/27506d11a58beb7be171d1c5ebabeedeb42de023/EventBusSample/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /EventBusSample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /EventBusSample/src/main/res/layout/fragment_main.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 |