I have two Fragments A and B. I am going from fragment A to Fragment B using Following method.
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new B()).addToBackStack(TAG).commit();
Now I want to send data to from fragment B to Fragment A i.e Previous Fragment.
Can you please give me an idea about how to send data to previous Fragment.
Thanks
Check the documentation about Fragments and its communication.
Do you want to pass info from one fragment to another? One fragment shouldn't know about other fragments. Make the fragment B communicate with its activity through an interface and make the activity pass the data to fragment A
You can communicate between fragments through Activity where you are replacing fragments.
Pass data to activity and then call method of that fragment where you want to update data.
2.Other way you can take a variable in Activity and save the data to this variable and when another fragment (Fragment A) is loaded(Resumed) then show data from Activity variable by getting
((HomeActivity)getActivity).variableName;
You create a subclass of Fragment called MyFragment that A, B are instances of it. Then create a static factory method take a MyFragment as param like:
public static MyFragment newInstance(MyFragment a){
MyFragment b = new MyFragment();
MyFragment b.a = a;
return b;
}
b.a is a field of MyFragment. Then you can use reference a to send data.
Related
I have a Activity which contains a fragment with recyclerview.
The recyclerview has few editable fields and need to put validations on clicking a button on activity.
Please let me know the solution or any techniques to overcome this problem statement
use this (click here ) functionality for data sharing between Fragment to fragment , Fragment to Activity and Activity to Activity
1) in your onCreate declare the BUSand register it
Bus bus = new Bus();
bus.register(this);
2) send data or any action using
bus.post(<your data or any Item>);
3) don't forgot to unregister in your onestroy
bus.unregister(this)
You can get that fragment instance either by 'ID' or by tag (you would need to give a tag while adding this fragment).
Now you can call any public method of that fragment using its instance.
For Example (Using tag), if you want to call a public method 'show()' of FragmentA. Then while adding it to the activity give it a tag like this:
Fragment fragmentA = new FragmentA();
getFragmentManager().beginTransaction()
.add(R.id.fragment_container,fragmentA,"YOUR_TARGET_FRAGMENT_TAG").commit();
And to get its instance (on a button click or as required):
FragmentA fragmentA = getFragmentManager().findFragmentByTag("YOUR_TARGET_FRAGMENT_TAG");
fragmentA.show();
Declare in your fragment the RecyclerView as public
Declare in your activity the fragment as variable
in your activity on click listener get the recyclerview with
fragment.recyclerview
I have an Activity. It adds Fragment A dynamically. Fragment A contains static added (by xml) child Fragment B. How can I pass data from Activity to Fragment B when i add Fragment A to Activity?
I know next approach:
Implement interface getter in Activity and call getter method from Fragment B
I'd like to know, are there another ways to do it?
I tried set arguments from Activity to Fragment A, find Fragment B by child fragment manager and set arguments from Fragment A to Fragment B.
But Fragment B created earlier than Fragment A, so i had exceptions..
Well there are more than one ways to accomplish that. one of the way is
Create a function public void setData(Data data) in your Fragment B
Transfer your data from Activity to Fragment A. Since your Fragment B is added by fragment tag in xml. create ref to your Fragment B object in Fragment A. Now you have the reference to Fragment B in A.
Now Simply call the function (created in Fragment B) in Fragment A and pass the data which you have received by the Activity
Other way..
In Fragment B you can call getActivity() and type cast it in to your Activity class type and call its method which can return the desired data
This sample by google should resolve all your Issues:
https://developer.android.com/training/basics/fragments/communicating.html
You can try EventBus to pass data from Activity to your Fragment B. In your activity call this EventBus.getDefault().post(new MessageEvent());
And then you just need to register EventBus in onCreateView of Fragment B
EventBus.getDefault().register(this);
And then
#Subscribe
public void onEvent(MessageEvent event){
// DO SOMETHING
}
Having Fragment A and B, B is kinda of extended A. The UI data update from B need to store back in A and A's UI makes decision whether to save to database.
So A is put on the backstack first, then at some time it needs B and B is put on the backstack and in the holder with 'replace()'.
Passing data to B could be don by argument.
How to pass data back from B fragment to the A fragment (if A is 'replaced()' by B on the holder and currently not showing or 'existing')?
Bundle args = new Bundle();
args.putSerializable(TAG_MY_CLASS, myClass);
Fragment bFrgmt = new B();
bFrgmt.setArguments(args);
getFragmentManager()
.beginTransaction()
.replace(R.id.holder, bFrgmt, TAG_B_FRAGMENT)
.addToBackStack(TAG_B_FRAGMENT).commit();
From Developers website:
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.
First send data from fragment to activity, then receive this data in activity, then from Activity you can send data to Fragment with intent and to receive in fragment onCreateView method.
You can communicate among fragments with the help of its Activity. You can communicate among activity and fragment using this approach.
I have added Fragment to Activity like
getSupportFragmentManager().beginTransaction()
.add(R.id.container,new MyFragment).commit();
where container is the id of FrameLayout
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Now how could i get the instance of Fragment in Activity like this
I have to call a method of Fragment A after getting result from Fragment B.
I have created an interface in Fragment B and implemented it in Activity.Now i have to pass the result to Fragment A. I am unable to get the instance of Fragment A.
One thing i don't wanna do is to create a private instance of Fragment A in Activity and call it's method.
Try this
getSupportFragmentManager().beginTransaction()
.add(R.id.container,new MyFragment(),"MyFragment").commit();
for get the fragment
MyFragment frag = ((MyFragment) getSupportFragmentManager().findFragmentByTag("MyFragment"));
Following this link:
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
Thus I would recommend:
To define a interface in Fragment B.
Implement the interface in activity.
Then eventually, deliver the message to Fragment A.
Code example and reference.
I have MainActivity with some buttons and I have different class with array of strings.
Can I send data (as I click on button) to a fragment so that it can choose from which class it takes the array strings?
If you instantiate your fragment in MainActivity:
MyFragment fragment = new MyFragment();
Then you can call methods from that fragment inside MainActivity like: fragment.myMethod(index_of_array);
If you do your fragments in XML, then you should find it by id.