如果要从Activity与Fragment进行调用,则从DialogFragment接收数据吗? [英] Receiving data from a DialogFragment if you're calling from an Activity vs a Fragment?
问题描述
我这样称呼我的DialogFragment:
I call my DialogFragment like so:
如果我正在参加活动:
MyDialogFragment dialogfragment = new MyDialogFragment();
dialogfragment.show(getFragmentManager(), "");
如果我已经在片段中:
MyDialogFragment dialogfragment = new MyDialogFragment();
dialogfragment.show(getActivity().getFragmentManager(), "");
在MyDialogFragment中,它会扩展XML并允许用户向EditTexts输入一些值,依此类推,我希望能够将这些值返回到我从中调用对话框的任何位置.
In MyDialogFragment, which inflates an XML and allows the user to input some values to EditTexts and so forth, I want to be able to return those values back to wherever I called the dialog from.
为了回答这个问题,让我们说我的对话框类想返回一些私有变量String mName
和int mValue
.
For the sake of the question let's say my dialog class wants to return some private variables String mName
and int mValue
.
在不知道从何处调用对话框(活动或片段)的情况下,是否存在正确的方法?如何将这些值传回/如何接收它们?
Is there a proper way to do this without knowing where the dialog is being called from (either an Activity or a Fragment)? How do I pass the values back / how do I receive them?
推荐答案
如果要从fragment
发送数据到activity
.您可以通过以下方式调用activity
的public
方法来做到这一点:
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);
将数据发送到fragment
不能做同样的事情.
You can't do the same for sending data to a fragment
.
如文档所述:
所有片段到片段"通信都是通过关联的活动"完成的.两个片段永远不要直接通信.
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
您应该做的是:
- 在片段中定义一个
Interface
. - 在
activity
中实现 - 将数据传递到
activity
- 然后
activity
会将数据传递到其他fragment
.
Interface
- Define an
Interface
in the fragment. - Implement that
Interface
in theactivity
- Deliver data to the
activity
- then
activity
will deliver data to some otherfragment
.
顺便说一句,您还可以使用此做法将数据发送到activity
(直到第3点).
BTW, you can also use this practice to send data to activity
(upto point 3).
定义接口:
public interface DataListener {
public void onDataReceived(Object obj);
}
在fragment
内:
public class MyFragment extends Fragment {
DataListener callback;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
callback = (DataListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement DataListener");
}
}
}
从fragment
发送数据;
callback.onDataReceived(object); // some object data
在activity
中接收数据:
public static class MainActivity extends AppCompatActivity
implements DataListener{
public void onDataReceived(Object object) {
// Do something here with object data
}
}
现在,如果需要,您可以将该数据发送到其他任何fragment
.
Now if you want, you can send this data to any other fragment
.
将数据从activity
发送到其他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.
}
这篇关于如果要从Activity与Fragment进行调用,则从DialogFragment接收数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!