Android best practices for interacting with fragmentation

I am new to Android Fragment and trying to find out the Fragment to Activity interface. What is the best approach (best practice) in Android for sharing activity fragmentation?

Let's say I have FragmentA and ActivityA. After the FragmentA popup appears on the screen, I would like to execute somemethod (possibly related to the UI) in ActivityA

Two (template) Solutions are possible here :

  • In FragmentA getActivity and turn on Activity in ActivityA, and then call somemethod.
  • In FragmentA, create an interface callback, and then implement this callback in ActivityA. Then in the callback, call somemethod.

Which template is more common / perfer in Android development and why. Or do you have an even better way of communicating from fragment to activity on Android to share with me?

Any comments, opinions and suggestions are welcome and welcome. ^^.

+6
source share
5 answers

The second solution is preferred because it allows your fragment to be more independent of its hosting activity.

If in the future you decide to place your fragment at another event, no changes will be required in the fragment, and you will only need to implement the interface in your activity.

I will add a third solution that uses an event bus (e.g. Otto), which also works, although some may argue that this makes your code a little less readable.

+5
source

The first method will be bad practice. The second method would work just fine, but your snippet will be closely related to your business.

There is another best approach - use some library of event events , for example, otto. Using this, you can effectively communicate with free communication in your activity and fragment.

+2
source

Your second approach is more flexible. You may not see huge benefits in one action and in one fragment. If you need to use the same fragment in another action, it will most likely break, discarding your activity. However, there is nothing wrong with the first approach, but it is a bit limited.

+1
source

The first pattern is best when your fragment is used in only one action. The second approach is necessary if you want your fragment to communicate with some other objects, and not with the activity that contains the fragment. If you always want to communicate with the hosting, a callback is not needed. Just create an interface and implement as many actions as possible. Then, getActivity () is returned in your drop activity.

MyInterface myInteface = (MyInterface) getActivity(); myinterface.somemethod(); 

You can even check if the activity implements the necessary interfaces, etc.

+1
source

The front-end approach works fine and is more flexible, because it does not bind your fragment to activity directly. One thing you should also consider is how much work may be required, i.e. She can manage multiple fragments. This tends to lead to bold fragments, as my question here was when I started using them.

+1
source

Source: https://habr.com/ru/post/975201/


All Articles