├── .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 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
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 |
17 |
18 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/EventBusSample/src/main/res/layout/fragment_no_sticky.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
20 |
21 |
27 |
--------------------------------------------------------------------------------
/EventBusSample/src/main/res/layout/fragment_second.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
13 |
14 |
20 |
21 |
28 |
--------------------------------------------------------------------------------
/EventBusSample/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/EventBusSample/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
7 |
--------------------------------------------------------------------------------
/EventBusSample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | EventBusSample
5 | Hello world!
6 | Settings
7 | Send normal Event
8 | Update Event
9 | Move to 3rd Fragment.
10 | Move to Second Fragment
11 | Move to no a sticky Fragment
12 | Fragment 1
13 | Fragment 2
14 | Fragment 3
15 | Fragment 4
16 | A normal event.
17 | I am waiting for a sticky, but it might not OK, when the sticky does not come, I can not refresh text.
18 | Simple Event(no onEvent() but onSimpleEvent())
19 |
20 |
--------------------------------------------------------------------------------
/EventBusSample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Sample to demonstrate [EventBus][1] which has inited by https://github.com/kevintanhongann/EventBusSample
2 |
3 | This Fork
4 | ---------
5 |
6 | Added a 3rd fragment to demonstrate more about normal events and sticky-events.
7 |
8 | 0. Refactoring codes on the original sample.
9 | 1. Use post() to update ActionBar's title.
10 | 2. Use post() to switch different Fragments.
11 | 3. Use postSticky() to update StickyEvent.
12 | 4. Update .gitignore and git-rm some objects.
13 | 5. Added BaseFragment to demonstrate coexistence between register and registerSticky.
14 | 6. Added NoStickyFragment that can handle normal event but unavailable for stickies.
15 |
16 | [1]: https://github.com/greenrobot/EventBus
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevintanhongann/EventBusSample/27506d11a58beb7be171d1c5ebabeedeb42de023/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun Aug 10 15:04:51 CEST 2014
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=http\://services.gradle.org/distributions/gradle-1.8-bin.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':EventBusSample'
2 |
--------------------------------------------------------------------------------