Fragment implementation - Using google's document as an example - android

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.

Related

Pass data and update fragments text/buttons on same host activity of NavigationUI

In the activity that hosts the navigation drawer I have the "Login" component on the menu which manages the google authentication. I want to propagate the user's info on another "Home" fragment and update its view with those infos.
QUESTION:
How do I pass data between two fragments of the same host activity in NavigationUI and how do I update the TextView of the "Home" fragment for once (since the view refreshes its default values all the time).
I tried with intents by specifying to the main activity the extras to receive so I can propagate them to the fragments, but it does not seem to work (getting the "Cannot find activity" error).
What am I missing?
Actually, you have asked multiple questions trying to answer all of them.
How do I pass data between two fragments of the same host activity
if you are using ViewModel, use LiveData to communicate between two fragments. - suggested approach
if you are not using ViewModel then use the interface callback approach.
examples of the above two are mentioned here.
https://developer.android.com/guide/fragments/communicate
How do I pass data between two fragments of the same host activity using NavigationUI
in the nav_graph.xml you can find the arguments section where you can mention the arguments you want to pass.
An example of that is available in the given link
https://developer.android.com/guide/navigation/navigation-pass-data
please add a comment if you want any more answers

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?

DialogFragment in Fragment and callbacks

I have read a lot of documents about how to use fragments but I have one more doubt.
I have one activity that controls 3 fragments, now in one of this fragment I call a DialogFragment and following the google's tutorial I defined an interface to the activity for callbacks. All it's works properly but is this the only way to pass data to the fragment that fired the Dialog? I think that it would be more convenient passing data directly to the fragment instead of passing from the activity. is there any method to do this?
Thank you in advance.

Communication among fragment and activity

I'm implementing fragments in my app. Referring to this documentation,
there is written I should use getActivity() to access activity methods but also (in the next paragraph) I should declare an interface in the fragment and let activity implement it.
Now, the second way is used for callback methods like events, but I can also use getActivity().onSomeEventHappened(), can't I?
Could someone explain me the differences? Because I cannot see differences among them.
There is no difference in the end result if you know that getActivity() will always return the type of Activity you expect.
However using interfaces is a good practice because it decouples your Fragments from a particular implementation of an Activity. So later on in the future if you decide to use your fragments with a different activity, all you have to do is have that activity implement your fragments Interface to be alerted of any fragment events.
You should always strive to have decoupled components if you want an application that is easy to extend without side effects.
You can not always simply call getActivity().onSomeEventHappened(). Just imagine this case: You have two fragments, one with ListView and other which shows image based on listItem selected. In second fragment you cannot just call getActivity().onListItemClicked(), because your activity has no such method, but if activity implements interface and catches those event from the first fragment, then you are able to pass info about event to the second fragment and how the right image.

How to call activity from another fragment.?

I already know how to make a fragment in Android. I want to open another activity in the same fragment on a button click event. The button is inside a fragment class.
How do I do that?
There are two possibilities depending on what you need:
Fragment Receive Result that demonstrates starting a new Activity from a Fragment, and receiving a result back from it.
setTargetFragment may be used, for example, if this fragment is being started by another, and when done wants to give a result back to the first. An example is available here
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
Here are some tutorials with example
http://mobile.tutsplus.com/tutorials/android/android-sdk-using-fragments/
http://developer.android.com/training/basics/fragments/communicating.html
EDIT: April 2013
I like #AlexLockwood's comment.
In the case that one fragment starts another fragment, it's fine to just use setTargetFragment(Fragment, int) and communicate with it directly by calling getTargetFragment(). Communicating with Fragments through the activity is usually a good idea because it keeps your fragments loosely coupled from one another.

Categories

Resources