Receiving Data From A Dialogfragment If You're Calling From An Activity Vs A Fragment?
Solution 1:
If you want to send data to activity
from fragment
. You can do that by calling public
method of activity
by:
((MainActivity)getActivity()).sendData(Object object);
You can't do the same for sending data to a fragment
.
As doc says:
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
What you should do is:
- Define an
Interface
in the fragment. - Implement that
Interface
in theactivity
- Deliver data to the
activity
- then
activity
will deliver data to some otherfragment
.
BTW, you can also use this practice to send data to activity
(upto point 3).
Defining an interface:
publicinterfaceDataListener {
publicvoidonDataReceived(Object obj);
}
inside the fragment
:
publicclassMyFragmentextendsFragment {
DataListener callback;
@OverridepublicvoidonAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented// the callback interface. If not, it throws an exceptiontry {
callback = (DataListener) activity;
} catch (ClassCastException e) {
thrownewClassCastException(activity.toString()
+ " must implement DataListener");
}
}
}
Sending data from fragment
;
callback.onDataReceived(object); // some object data
Receiving data in activity
:
publicstaticclassMainActivityextendsAppCompatActivityimplementsDataListener{
publicvoidonDataReceived(Objectobject) {
// Do something here with object data
}
}
Now if you want, you can send this data to any other fragment
.
Sending data from activity
to some other fragment
:
AnotherFragment anotherFrag = (AnotherFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_container);
if (anotherFrag != null) {
anotherFrag.receiveDataInFrag(object);
}else{
// create a new instance of the fragment and pass data to it.
}
Solution 2:
Create a callback interface and have pass it into your dialogfragment
interfaceDialogValuesCallback {
voidcallThisFunctionWhenUserClicksOnOkInDialog(String passinmName,int passinmValue);
}
You can have your Activity or Fragment implement this interface.
Have a constructor in your MyDialogFragment which accepts the interface and assigns it to an member variable.
MyDialogFragment(DialogValuesCallback activityOrFragmentWhichImplementsThis){
mInterfaceCallbackObjectRef = activityOrFragmentWhichImplementsThis;
}
Solution 3:
@Rohit Arya this is what you should know first that fragments needs to declare an interface that must be implemented by every activity that uses that fragment so you can pass data from the fragment to the activity(s)... and must cast the activity displaying the fragment currently into this interface like this in your onAttach method
//@param Listener is the declared interface in your fragment class public class MyFragment extends Fragment { Listener mInterface;
@OverridepublicvoidonAttach(Activity activity) {
super.onAttach(activity);
try {
mInterface = (Listener) activity;
} catch (ClassCastException e) {
thrownewClassCastException(activity.toString()`
+ " must implement DataListener");
}
}
publicinterfaceListener{
//@param type = data type, cont = variablepublicvoidsendData(type cont, type2 cont2)
;
}
}
make sure the interface declared in your fragment get implemented in your baseActivity then what ever data you pass to the interface using
mInterface.sendData(value, value1);
you can get it in the base activity like
publicclassbaseActivityextendsActivityimplementsMyFragment.Listener{
//@param cont & cont2 are data sent from MyFragment@OverridepublicvoidsendData(type cont, type cont2){
//do what ever with your values here
}
}
got it now?
Post a Comment for "Receiving Data From A Dialogfragment If You're Calling From An Activity Vs A Fragment?"