The Android documentation suggests that to communicate from an activity to a hosted fragment, the fragment can define a callback interface and require that the host activity implement it. The basic pattern involves implementing onAttach
in your fragment, and casting the activity to a callback inteface. See http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity.
Here's an example of providing a fragment some initialization data, as well as listening for a navigation callback.
public class HostActivity extends Activity implements FragmentHost { @Override UiModel getUiModel() { return mUiModel; } @Override FragmentNavListener getNavListener() { return mNavListener; } ... } public class HostedFragment extends Fragment { @Override public void onAttach(Activity activity) { super.onAttach(activity); if (activity instanceof FragmentHost) { FragmentHost host = (FragmentHost) activity; setUiModel(host.getUiModel()); setNavListener(host.getFragmentNavListener()); } } ... }
Compare this to using onAttachFragment
in the host activity to explicitly initialize the fragment:
public class HostActivity extends Activity { @Override public void onAttachFragment(Fragment fragment) { super.onAttachFragment(fragment); if (fragment instanceof HostedFragment) { HostedFragment hostedFragment = ((HostFragment) fragment); hostedFragment.setUiModel(mUiModel); hostedFragment.setNavListener(mNavListener); } } ... }
To me, it seems like the first pattern has some drawbacks:
For those of you who've done activity to fragment communication, what pattern do you find preferable, and why? Are there drawbacks to using onAttachFragment
from the host activity?
Activities are an ideal place to put global elements around your app's user interface, such as a navigation drawer. Conversely, fragments are better suited to define and manage the UI of a single screen or portion of a screen. Consider an app that responds to various screen sizes.
onAttach(Activity) called once the fragment is associated with its activity. onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
I cant speak personally with respect to testing but there is alternatives to fragment / activity callback interface communication.
For example you can use a event bus to decouple the fragments and your activity. An excellent event bus can be found here:
Otto - An event Bus by Square
It is actively being developed by some very talented engineers at Square. You can also use the LocalBroadcastManager that is packaged in the Android Support Library.
LocalBroadcastManager
Eric Burke from square has a presentation where he mentions both which can be found here:
Android App Anatomy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With