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

Asked

Viewed 608 times

3

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

1st option : I’m stating directly in Fragment the getActivity():

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

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

public void mensagemToast();

Then in the Activity I have implemented this method using the this(Activity):

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

    }

Then I created a Listener no Fragment to call this method that is implemented in the Activity:

listener.mensagemToast();

This is just one example I have several cases similar to this that in Fragment I call directly the getActivity(). What is the best form among the options I presented?

1st that seems to be more simple but, is picking up a direct reference from Activity?
2nd that is more bureaucratic but decoupled (separate) from Fragment?
Or is there another solution that is better than the above?

1 answer

4


A Fragment must be implemented in a way that is self-sufficient, must be able to function/achieve its objective on its own.
Following this principle the Fragment can be used by any Activity.

This is one of the main benefits of using Fragments.

For this, the so-called methods of the Activity on the part of Fragment, should be done only in order to inform the Activity that something has happened, and to that end a interface that the Activity should implement.

The Fragment can then check whether the Activity whether or not it implements the interface, deciding whether or not to be created.
In the first case, it does not call any of the interface methods.
In the second, he makes an exception.

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

My approach is as follows::

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

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

In the method onAttach() these attributes are used to store the context and a reference to interface implemented in Activity who 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 one context use the attribute context.
When I need to call a method of Activity use container.nomeMetodo();

See the documentation of android as regards the Design Philosophy

  • um... very good your answer... but I’m still doubtful only on the part of choosing between using without/with interface in the example I gave.. or in my question I must choose the first option or the second?

  • 2nd option - Use a interface

  • blz vlw +1 in his reply

  • just disturbing one more time.. in case I create a Alertdialog.Builder Builder = new Alertdialog.Builder(getActivity()); in Fragment it would also be necessary to create an interface, that is, whenever in Fragment use getActivity() I should always use an interface?

  • I don’t know what you mean by that example. But when you ask if "always must", my answer is: depends on the context. Standards and good practice should not be followed just because someone says so. Context must be taken into account: the benefit must be greater than the cost.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.