Last active
January 2, 2016 18:49
-
-
Save granoeste/8346665 to your computer and use it in GitHub Desktop.
[Android] Run the post method of the Otto Bus in the Main thread always. And Discussion the Observer/Observable(Callback) pattern. - Case01: Activity/Fragment using a dedicated callback. (the observer pattern) - Case02: Activity/Fragment using a common callback. (the observer pattern) - Case03: Activity/Fragment using a LocalBroadcastManager. - …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.os.Handler; | |
import android.os.Looper; | |
import com.squareup.otto.Bus; | |
public final class BusProvider { | |
private static final Object sLock = new Object(); | |
private static MyBus sInstance; | |
public static MyBus getInstance() { | |
synchronized (sLock) { | |
if (sInstance == null) { | |
sInstance = new MyBus(new Handler(Looper.getMainLooper())); | |
Log.i("BusProvider", "The bus was ready."); | |
} | |
return sInstance; | |
} | |
} | |
private BusProvider() { | |
// No instances. | |
} | |
public static class MyBus extends Bus { | |
private Handler mHandler; | |
public MyBus(Handler handler) { | |
super(); | |
mHandler = handler; | |
} | |
@Override | |
public void post(final Object event) { | |
if (Looper.myLooper() != mHandler.getLooper()) { | |
mHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
MyBus.super.post(event); | |
} | |
}); | |
} else { | |
super.post(event); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FirstFragment extends Fragment { | |
OnListener mCallback; | |
public interface OnListener { | |
public void onFirstCallback(); | |
} | |
@Override | |
public void onAttach(Activity activity) { | |
super.onAttach(activity); | |
if (activity instanceof OnListener) { | |
mCallback = (OnListener) activity; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends FragmentActivity | |
implements FirstFragment.OnListener, SecondFragment.OnListener { | |
FirstFragment mFirstFragment; | |
SecondFragment mSecondFragment; | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (savedInstanceState != null) { | |
return; | |
} | |
mFirstFragment = new FirstFragment(); | |
mSecondFragment = new SecondFragment(); | |
getSupportFragmentManager().beginTransaction() | |
.add(mFirstFragment, "first_frag") | |
.add(mSecondFragment, "second_frag") | |
.commit(); | |
} | |
@Override | |
public void onFirstCallback() { | |
} | |
@Override | |
public void onSecondCallback() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SecondFragment extends Fragment { | |
OnListener mCallback; | |
public interface OnListener { | |
public void onFirstCallback(); | |
} | |
@Override | |
public void onAttach(Activity activity) { | |
super.onAttach(activity); | |
if (activity instanceof OnListener) { | |
mCallback = (OnListener) activity; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FirstFragment extends Fragment { | |
OnFragmentEvenCallback mCallback; | |
@Override | |
public void onAttach(Activity activity) { | |
super.onAttach(activity); | |
if (activity instanceof OnFragmentEvenCallback) { | |
mCallback = (OnFragmentEvenCallback) activity; | |
} | |
} | |
private void sendMessage() { | |
Bundle bundle = new Bundle(); | |
bundle.putString("kind", "first"); | |
bundle.putInt("status", 0); | |
mCallback.onFragmentEvent(bundle); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends FragmentActivity | |
implements OnFragmentEvenCallback { | |
FirstFragment mFirstFragment; | |
SecondFragment mSecondFragment; | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (savedInstanceState != null) { | |
return; | |
} | |
mFirstFragment = new FirstFragment(); | |
mSecondFragment = new SecondFragment(); | |
getSupportFragmentManager().beginTransaction() | |
.add(mFirstFragment, "first_frag") | |
.add(mSecondFragment, "second_frag") | |
.commit(); | |
} | |
@Override | |
public void onFragmentEvent(Bundle bundle) { | |
String kind = bundle.getString("kind"); | |
if (kind.equals("first")) { | |
int status = bundle.getInt("status"); | |
// ... | |
} else | |
if (kind.equals("second")) { | |
MyObject payload = (MyObject) bundle.getSerializable("payload"); | |
// ... | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface OnFragmentEvenCallback { | |
public void onFragmentEvent(Bundle bundle); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SecondFragment extends Fragment { | |
OnFragmentEvenCallback mCallback; | |
@Override | |
public void onAttach(Activity activity) { | |
super.onAttach(activity); | |
if (activity instanceof OnFragmentEvenCallback) { | |
mCallback = (OnFragmentEvenCallback) activity; | |
} | |
} | |
private void sendMessage() { | |
Bundle bundle = new Bundle(); | |
bundle.putString("kind", "second"); | |
bundle.putSerializable("payload", mMyObject); | |
mCallback.onFragmentEvent(bundle); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FirstFragment extends Fragment { | |
private void sendMessage() { | |
Bundle bundle = new Bundle(); | |
bundle.putString("kind", "first"); | |
bundle.putInt("status", 0); | |
Intent intent = new Intent("custom-event"); | |
intent.putExtras(bundle); | |
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends FragmentActivity { | |
FirstFragment mFirstFragment; | |
SecondFragment mSecondFragment; | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (savedInstanceState != null) { | |
return; | |
} | |
mFirstFragment = new FirstFragment(); | |
mSecondFragment = new SecondFragment(); | |
getSupportFragmentManager().beginTransaction() | |
.add(mFirstFragment, "first_frag") | |
.add(mSecondFragment, "second_frag") | |
.commit(); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, | |
new IntentFilter("custom-event")); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
LocalBroadcastManager.getInstance(this.unregisterReceiver(mMessageReceiver); | |
} | |
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Bundle bundle = intent.getExtras(); | |
String kind = bundle.getString("kind"); | |
if (kind.equals("first")) { | |
int status = bundle.getInt("status"); | |
// ... | |
} else | |
if (kind.equals("second")) { | |
MyObject payload = (MyObject) bundle.getSerializable("payload"); | |
// ... | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SecondFragment extends Fragment { | |
private void sendMessage() { | |
Bundle bundle = new Bundle(); | |
bundle.putString("kind", "second"); | |
bundle.putSerializable("payload", mMyObject); | |
Intent intent = new Intent("custom-event"); | |
intent.putExtras(bundle); | |
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FirstFragment extends Fragment { | |
int mStatus; | |
private void sendMessage() { | |
BusProvider.getInstance(getActivity()).post(produceFirstFragmentEvent()); | |
} | |
// The method should take no parameters and its return type will be used as the type of event | |
@Produce | |
public FirstFragmentEvent produceFirstFragmentEvent() { | |
return new FirstFragmentEvent(mStatus); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
BusProvider.getInstance(getActivity()).register(this); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
BusProvider.getInstance(getActivity()).unregister(this); | |
} | |
@Subscribe | |
public void onSecondCallback(SecondFragmentEvent event) { | |
int status = event.status; | |
MyObject payload = event.payload; | |
// ... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FirstFragmentEvent { | |
int status; | |
public FirstFragmentEvent(int status) { | |
this.status = status; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends FragmentActivity { | |
FirstFragment mFirstFragment; | |
SecondFragment mSecondFragment; | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (savedInstanceState != null) { | |
return; | |
} | |
mFirstFragment = new FirstFragment(); | |
mSecondFragment = new SecondFragment(); | |
getSupportFragmentManager().beginTransaction() | |
.add(mFirstFragment, "first_frag") | |
.add(mSecondFragment, "second_frag") | |
.commit(); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
BusProvider.getInstance(getActivity()).register(this); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
BusProvider.getInstance(getActivity()).unregister(this); | |
} | |
@Subscribe | |
public void onFirstCallback(FirstFragmentEvent event) { | |
int status = event.status; | |
// ... | |
} | |
@Subscribe | |
public void onSecondCallback(SecondFragmentEvent event) { | |
int status = event.status; | |
MyObject payload = event.payload; | |
// ... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainApplication extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
BusProvider.getInstance(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SecondFragment extends Fragment { | |
Bus bus = BusProvider.getInstance(); | |
private void sendMessage() { | |
int status; | |
MyObject payload; | |
bus.post(new SecondFragmentEvent(status, payload)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SecondFragmentEvent { | |
int status; | |
MyObject payload; | |
public SecondFragmentEvent(int status, MyObject payload) { | |
this.status = status; | |
this.payload = payload; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment