Android Fragments fundamentals: why? Is this conceptually wrong? Android Fragments fundamentals: why? Is this conceptually wrong? android android

Android Fragments fundamentals: why? Is this conceptually wrong?


Ill do my best to answer the wall of text here :)

Question A:

Fragments are designed to be reusable modules that can be plug and played with any activity. Because of this the only correct way way to interface with the activity is to have the activity inherit from a interface that the fragment understands.

public class MapFragment extends Fragment {  private MapFragmentInterface listener;   public interface MapFragmentInterface {      //All methods to interface with an activity  }  @Override  public void onViewCreated(View view, Bundle savedInstanceState) {      // sending the event      if (listener != null) listener.anyMethodInTheAboveInterface();  }}

Then have the activity implement the interface

public class MainActivity extends Activity implement MapFragmentInterface{//All methods need to be implemented here}

This allows your fragment to be used with any activity as long as the activity implements this interface. The reason why you need this interface is because the fragment can be used with any activity. Calling a method like

((TestActivity) getActivity().myCustomMethod();

relies on the fact that your fragment only can work within a test activity and therefore "breaks" the rules of fragments.

Question B and C:

Assuming that you are following the correct guidelines for fragments and that they are independent modules. Then you should never have a situation where fragments need to know about each other. 99% of the time that people think they need fragments to directly communicate they can re-factor their problem to the situation I gave above by using a MVC patten or something similar. Have the activity act like the controller and tell the fragments when they need to update and then create a separate data store.


I will try to clear it all a little bit.

First of all, consider you approach of setting listener for fragment. It is no good to set listener in onViewCreated method, because it leeds to excess reseting listener any fragment is created. It is enough to set it up into onAttach method.

I told about code lines. Take me to notice, it is good to have BaseFragment implemented common behavior in you app as setting FragmentListener creating view from resource.

And more than that to reduce code lines and to get part of code reuse you can use generic in BaseFragment. So look at next code snippet:

public abstract BaseFragment<T extends BaseFragmentListener> extends Fragment {  T mListener;  public void onAttach(Activity activity) {    super.onAttach(activity);    if (Activity instanceof T)      mListener = (T) activity;   }  abstract int getLayoutResourceId();  @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View layout = inflater.inflate(getLayoutResourceId(), null);        // you can use some view injected tools here, mb ButterKnife         return layout;    }}

Answer A (For Question A):

If you have fragment only exactly one activity you need to decide: "Do you really need to use Fragment here?". But mb it is good to have fragment exactly for one activity to extract some view logic from activity and clearing base logic. But for clearing base architecture logic for your app use Listeners. This will make life easier for other developers

Answer B:For nested fragments you need to solve, what they need to use exact activity or just fragments and use it as a bridge to other system. If you know that nested fragment will be nested all time you, you need to declare parent fragment as listener otherwise you have to use other approach.

Notice:As the base approach to communicate between diff part of App you can use events, try too look at event bus for example. It gives you common approach for communication and you can extract logic of calling custom methods of listeners and more another, all logic will be situated into handling events and you will have one mediator system for cooperation.

Answer C:I partially explain one of approaches to cooperating between fragments. Using one event dispatcher avoids you to have many listeners for all different communication. Some times it is very profitably.

Or I think it is more usable to use Activity, or other class lives in Activity, for a mediator for Fragments cooperation because there is many situation of Fragments changing during lifecycle both handling and system. And it focuses all this logic in one place and makes your code more clear.

Hope my considerations helps you.