How to make callBack response from one fragment to another fragment? - android

Sir/Madam,
I want to make a callback result to one fragment to another fragment using android JAVA.
I try-out to find the issue but non of the solution found.
If i using interface it send callback response to Activity, which i didn't want.
thank you!

You have many options to do so:
Shared ViewModel (Recommended) (https://medium.com/#abhilashmyworld/communicate-between-fragments-using-viewmodel-e83344e9df53)
EventBus
Through the Hosting activity (calling method on fragment object)
Passing objects
Broadcast (too old school)

Related

How to open multiple instances of the same fragment but with different data, one after the other?

I made several api calls from a model class of an Activity and upon receiving responses from each of them, I need to feed the data to a Fragment one after the other by invoking multiple instances of the same fragment.
Ideally, the next fragment will only be fed with data after the previous Fragment has exited (by response from a listener).
I have looked everywhere and couldn't find a solution to this problem. I have tried using AsyncTask with a CountDownLatch to block the next api response before getting an action response from the initial Fragment but after it only invoked one Fragment (I know how many fragments I should be creating) and back to normal Activity view.
Any thoughts on how to deal with this problem?
There are 2 ways for this
Create a constructor to the fragment where you pass the data when you create instances.
mFragment1 = new xFragment(dataA);
mFragment2 = new xFragment(dataB)
To use Interface and Implementation to pass data from Activity to. Check the link below
https://developer.android.com/training/basics/fragments/communicating.html

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?

Android Fragments: how do I save and pass data back to parent activity once the user closes the current activity?

I have a SearchFragment and a PersonFragment which are hosted by different FragmentActivitys
The user will navigate from the SearchFragment to the PersonFragment. When the user is done with the PersonFragment (such as when they press the back button), I would like to send data back to SearchFragment so it can update its UI with any changes the user made while in PersonFragment.
I read the best way to do this is in the Activity's finish() method. However, since I'm using fragments I'm not sure how to accomplish this.
You first communicate from PersonFragment to the associated activity using interface as a callback.
Then you can use intent to pass values between activities. From activity you can communicate to SearchFragment.
http://developer.android.com/training/basics/fragments/communicating.html
According to the tutorial on the Android developers site (link), you should declare a listener interface in your PersonFragment, implement it in your FragmentActivity and in that implementation, update your SearchFragment's UI.
Edit: Another approach would be to use LocalBroadcastManager, if you can package your relevant data in an Intent, this approach uses a bit less boilerplate code.
you dont need it, all you need to do is some ContentObserver that notifies you about the content changes, see ContentResolver and its methods: registerContentObserver() and notifyChange()
Ended up using EventBus for this

Roboguice Fragement pass data

I'm using RoboGuice in my Android application, I'm not sure how to pass object between
Activity to Fragment?
I am aware about an approach where in you can serialize and pass in the bundle. In this article
How to pass data between fragments in one of the comments it is mentioned
If you use Roboguice you can use the EventManager in Roboguice to pass data around without using the Activity as an interface. This is
quite clean IMO.
I'm not sure how to do that, can anyone give an example?
Since Fragments are contained within an Activity, and you normally have a reference to it, it's pretty common for a Fragment to expose either a specific constructor or setter methods that the Activity can invoke. There's no need for serialization. To got the other way (Fragment -> Activity) the Fragment should expose an interface that the Activity can listen for events on.
I don't think that approach is unique to roboguice.
I prefer event propagation as opposed to the (perfectly acceptable) callback approach outlined by dmon. I have provided a mini-guide here...I suspect roboguice's version is very similar. 2 components (activity + fragment), fragment broadcasts a 'I did this' message whilst the activity declares 'I'm listening for that'...

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.

Categories

Resources