How to pass data from Activity to fragment inside fragment - android

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
}

Related

Activity to RecyclerView Adapter Communication(Recyclerview in Fragment)

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

Accessing the button id of fragment A from fragment B

I have created Fragment A and Fragment B , which has separate buttons on each fragment , I need to enable the button on fragment B until and unless button on fragment A is clicked , how can I do this as I am new to this android .
There are 3 ways :
create public static variable and access it from other fragment
use custom BroadcastReceiver and call it when button of fragment1 is called, and receive that in fragment2.
use EventBus, call the event and receive it in another fragment.
when you have to click on fragment A button then enable the button on fragment B in on attach or on create method here you need to use interface.
Define an Interface in fragment A
Implement the Interface in fragment B
onClick of Button in Fragment A Call interface, and message is delivered to fragment B.

Get Fragment instance in Activity

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.

Pass data to previous fragment

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.

If theres multiple fragments after fragment remove from backstack, which method runs

If there's multiple fragments one on another, after one fragment remove from the stack, which method runs in the below fragment.
This is whats happens in the program,
Fragment A calls -> Fragment B
Then I remove Fragment B. So now its in the fragment A. I in that moment I want to run a method in Fragment A.
I tried onResume in Fragment A. But its not working. Can anyone please suggest a method to achieve this approach.
You can either use Callbacks to communicate between fragments, or use broadcast receivers
To use callbacks...
In Fragment B declare an interface like so
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void myMethod();
}
Also in fragment B create a global instance of our OnFragmentInteractionListener :
private OnFragmentInteractionListener mListener;
Then whenever you want the method in Fragment A to be called just use :
mListener.myMethod();
Now in Fragment A we need to provide the myMethod and implement our listener from Fragment B :
public class FragmentA extends Fragment implements FragmentB.OnFragmentInteractionListener
And also in Fragment A provide the method
public void myMethod(){
//Hello World
}

Categories

Resources