When I have in Fragment a reference through getActivity () should I use interface?

3

My question is how best to work for the organization of the methods, I will present 2 implementation options that I am in doubt, whenever I have in Fragment a reference through getActivity () should I use interface? :

1st choice: I'm declaring directly in Fragment the getActivity () :

Toast.makeText(getActivity(), "mensagem", Toast.LENGTH_LONG).show();

2nd option: I created an interface with the method:

public void mensagemToast();

Then in Activity I implemented this method using this (Activity) now:

@Override
    public void mensagemToast() {
        Toast.makeText(this, "mensagem", Toast.LENGTH_LONG).show();

    }

Then I created a listener in Fragment to call this method that is implemented in Activity:

listener.mensagemToast();
  

This is just an example I have several cases similar to this that in Fragment I directly call the getActivity () . What is the   best way out of the options I presented?

     
    

1st that seems to be over     simple but, are you taking a direct reference to Activity?
2nd that is more bureaucratic but unbound (separate) from Fragment?
Or is there another solution that be better than the ones above?

  
    
asked by anonymous 19.10.2014 / 12:14

1 answer

4

A fragment should be implemented in order to be self-sufficient, it should be able to work / achieve its purpose by itself. Following this principle fragment can be used by any activity .

This is one of the key benefits of using fragments .

To do this, the activity methods call should be to inform activity that something happened, and for that purpose an interface activity you should implement.

Frag can then check whether the activity in> interface , deciding whether or not to be created. In the first case, it does not call any of the interface methods. In the second, it throws an exception.

In my fragments I never use getActivity() . If I need a Context , I get it in the onAttach() method, if I need to communicate with the Activity, I have it implement an interface .

My approach is as follows:

I declare interface and two attributes in the derived class of Fragment

public interface Container{
    //Métodos que as actividades devem implementar
}
private Container container;
private Context context;

In the onAttach() method, these attributes are used to store context and a reference to the interface implemented in activity that created the fragment >

@Override
public void onAttach(Activity activity) {

    if(activity instanceof Container){
        container = (Container) activity; //Guarda uma referência à actividade(interface)
    }
    else{
        throw new ClassCastException(activity.toString()
                  + " A actividade deve implementar a interface Container");
    }

    context = activity; //Guarda context.

    super.onAttach(activity);
}

When I need a context I use the context attribute. When I need to call a method of activity use container.nomeMetodo();

See the android documentation for Design Philosophy a>

    
19.10.2014 / 16:13