This question already has answers here:
How to get data from DialogFragment to a Fragment?
(7 answers)
Closed 6 years ago.
I have a fragment and a dialogfragment, I want to communicate between them:
send data from fragment to dialogfragment and
send data from dialogfragment to fragment
I read about setTargetFragment and getTargetFragment but I couldnot understand how to use them correctly to send/receive data to/from dialogfragment.
any help?
There's answer for this already. Look at this for Communication between Fragment and DialogFragment - if this helps you, you can accept the answer, otherwise you should motivate why your specific problem is different from the one presented in here.
To see how to use the setTargetFragment and getTargetFragment, you can look at the [selected answer here] (How to get data from DialogFragment to a Fragment?) - specifically, you might find the code helpful for what you want to do.
So, I have Communicated from DialogFragment to Fragment using this way
and Communicated from Fragment to dialogFragment using setArguments() and Bundel
Related
I have two activities with fragment1 inside activity1 and fragment2 inside activity2
fragment1 and fragment2 extend from Fragment
activity1 and activity2 extend from AppCompatActivity
i want to send String value from fragment1 to fragment2 without getting Null value. I hope you understand the problem.
thanks
Best way to achieve this is using singleton pattern, you can use it for fragment-fragment, fragment-activity communication.
See this answer, it explained with code there and in a very meaningful way. but what i preferred from my experience is you should user EventBus if you want to handle bigger projects with many fragments and larger dependencies, connections and data flows within and between fragments and activities.
The basic communication can be done as per suggested in Developer documentation here
This question already has answers here:
How to pass values between Fragments
(18 answers)
Closed 7 years ago.
What is the best way to pass data from one fragment to another in android ? I know Otto and Event Bus can handle the issue, but what exactly should the non-library way to do out?
Please consider following before answering
http://developer.android.com/training/basics/fragments/communicating.html
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
You can do it using Bundle
MyFragment fragment = new MyFragment ();
Bundle data= new Bundle();
data.putString("Key", "Value");
fragment .setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container, fragment ).commit();
Retrieve the bundle in another fragments onCreate by
String value = getArguments().getString("Key");
If you don't use Otto / EventBus then you should use listener interfaces.
Implement a listener in your MainActivityand pass the value to another fragment from the MainActivity. Fragments are generally used as reusable components and it is always better to reduce the coupling between fragments using listener interfaces in your MainActivity.
This question already has answers here:
What is the difference between Fragment and FragmentActivity?
(4 answers)
Closed 9 years ago.
I am new in Android programming. I want to do a Fragment, I have seen examples that uses Fragment and FragmentActivity, What's the difference between them and which cases should be used each one?
Thanks
FragmentActivity is our classic Activity with fragment support, nothing more. Therefore FragmentActivity is needed, when a Fragment will be attached to Activity.
Well Fragment is good component that copy the basic behaviors of Activity, still not a stand-alone application component like Activity and needs to be attached to Activity in order to work.
Read here Difference between Fragment And FragmentActivity
FragmentActivity is part of the google-support-v4 lib which is basically adds a Fragment support to system with OS under 2.3. So FragmentActivity is exactly as a simple Activity only it gives you the ability to add Fragment to it.
Fragment is an object that shares parts of the Activity life cycle and can be added as part of you UI to an Activity or FragmentActivity with it's logic. the beauty of Fragments is that can be reused across different Activities in your application.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ViewPager Activity to notify a Fragment of a specific event
I am experimenting with using fragments and I ran into this problem.
I have a service running in the background which I want to update a listview in a fragment.
First I just wanted to have the fragment listen for a broadcast, but it seems like registerReceiver is not allowed in a fragment?
How do I do this. Do I need to listen for a broadcast in the main activity and then somhow send it to the fragment?
I'd use the activity for this. Google has some good examples and description of the topic fragment and activity communication. You can read about it here: http://developer.android.com/training/basics/fragments/communicating.html
In short, fragments use listeners to communicate with activities and activities just use public methodes in fragments.