Why use an interface to communicate from fragment to activity? - android

What is the advantage of using an interface to communicate from a fragment to an activity, as described here: http://developer.android.com/training/basics/fragments/communicating.html
This creates an unecessary dependency when we could have created an "onArticleSelected()" method in the activity WITHOUT THE INTERFACE and called it in the fragment via getActivity().onArticleSelected().
What if the activity, at another point in time contain a fragment where there are no articles, why create this illogical dependency and add more code?

Using an interface actually removes dependency on a specific Activity class. It allows the Fragment to work with any Activity that implements the interface, not just a single Activity.

Related

Best way to handle multiple fragments (nested)

In my Android application I have an activity with a Navigation Drawer Each menu item click open a fragment.
This transactions are done from the activity.
In one fragment I have a button that open a new fragment when it is clicked.
I have doubts about how to handle this nested fragment:
The simple approach would be to copy/paste my openFragment method from the activity.
Other approach could be to create an interface with a callback to MainActivity openFragment method.
Which is better approach?
The best way to communicate between your activity and fragments is using interfaces.
You can define the interface inside your fragment and implement the interface in the activity.
Once the activity implemented the interface,you can communicate or write any business logic within the method that the activity overrides.
Google also recommends the same:
use EventBus to post the event from fragment and activity handle the event the deal with event.

trying to have a fragment talk to its parent Activity when the fragment is in a share module

I am trying to have my fragment (in a share module) comunicate with its parent activity.
I tried using the following code to call a callback in the parent activity
((MainActivity)getActivity())
I got a error saying MainActivity is undefined. I'm assuming this is because the fragment and Activity are in different modules. (NyLibary and app)
As it's in another module you'll need to import MainActivity class in your fragment.
By the way calling the parent activity this way is not the best thing you can do, in my opinion you should have interface represents the One who should implement your functions, this way you save the modularity for your fragment.

android - Why should I implement interface while communicating between main activity and fragment

I do not understand something. Now, I am reading the documents in android developer site and there, it is written that in order to communicate with fragments, I should implement interface. However, now I can easily access the widgets, exist on fragment, in Main activitiy class.
For example, in main activity class, by issuing following line, I can access fragment´s TextView.
TextView t1 = (TextView) findViewById(R.id.t1);
In this condition, why do i need to implement interface? (forgive my ignorance and thanks)
On Fragment, how do you access a method of the Activity it is attached to? You could call getActivity() but that will only give access to the methods available to the parent Activity object, not your implementation of it with your own custom methods.
To access those custom methods, you need to tell Java that you know that the particular Activity you want to get is the one you created, lets call it MyActivity, which obviously extends Activity or some other implementation of it like ActionBarActivity. In this case you can call ((MyActivity)getActivity()).myMethod();
The problem now is that you are coupling your fragment to that particular activity. By doing so, you won't be able to use your fragment with any other activity in your project because that particular fragment will be looking for MyActivity.
So instead, you declare myMethod() in an interface and make your Activity implement it. If in the future you need to use the fragment with another Activity, all you have to do is make it implement the interface as well.

Fragment implementation - Using google's document as an example

I am reading google developer's article on Fragment implementation. I am stuck at one point in the session "Creating event callbacks to the activity".
It says that when Fragment A, which contains a list of articles' title, wants to communicate with Article Fragment, which shows the content of the article, Fragment A should implement an interface OnArticleSelectedListener. A listener is set in the activity hosting the two fragments, and the listener will send information to Article Fragment.
My problem is that, why don't we simply implement an interface in Article Fragment listening to Fragment A's selection? I know there must be a reason, but I just don't get it.
My Attempt:
Is it because we cannot find a reference to Article Fragment in Fragment A? We could only find the reference of Article Fragment from the hosting activity.
Many Thx.
Fragments can not communicate with each other. Activity is not only a Host but also acts as a middle man between two or more fragments.
(taken from developer.android.com)
Although a Fragment is implemented as an object that's independent from an Activity and can be used inside multiple activities, a given instance of a fragment is directly tied to the activity that contains it.
In some cases, you might need a fragment to share events with the activity. A good way to do that is to define a callback interface inside the fragment and require that the host activity implement it. When the activity receives a callback through the interface, it can share the information with other fragments in the layout as necessary.

What is the difference creating event callback or the activity itself within a fragment?

Lets say I will be using several fragments(Action1Fragment, Action2Fragment etc.) within an activity(ActionActivity). I want to access some elements of activity object, or call some methods of ActionActivity. It is generally offered to create a event callback . What if I keep a reference to ActionActivity within Action1Fragment instead of keeping a reference to CallBackInterface which is actually implemented by ActionActivity since I will be using these fragments only within a particular activity.
I am kinda confused by the idea that Activity might be dead while reference of interface might still be alive(it sounds ridiculous when I read it again but it is OK if I managed to explain myself).
The Android Developer tutorials recommend that you use a callback interface on your fragments. The activity that hosts the fragment must implement the callback interface. The fragment does getActivity() and casts it to the callback interface, and then makes the callback.
This is the recommended way to promote a more modular design. It would not matter if your fragments will only ever work inside one activity. But if you want to make more generic fragments that could be used by different activities, then the above design pattern starts to become useful. (For example: a telephones fragment inside an person fragment and a company fragment.)
Suppose you do it the other way: the fragment does getActivity() and casts it to PersonActivity. The fragment then has access to all the public methods of PersonActivity. But this design pattern becomes much more ugly when you need the other activity to also use the fragment. The fragment would then have to be changed to first try and cast to PersonActivity, and if that throws, try the CompanyActivity.
The recommended design pattern basically gives you a way to make an activity compatible with the fragment instead of vice versa. The fragment only knows about the callback interface and not about any of the activities itself. The activities do know about the fragment because they implement the callback interface but they already knew about it because they constructed and initialized an instance of it.
Does that make sense?

Categories

Resources