如果您从 Activity 与 Fragment 调用,是否从 DialogFragment 接收数据?
Receiving data from a DialogFragment if you're calling from an Activity vs a Fragment?
我这样称呼我的 DialogFragment:
如果我在 Activity:
MyDialogFragment dialogfragment = new MyDialogFragment();
dialogfragment.show(getFragmentManager(), "");
如果我已经在片段中:
MyDialogFragment dialogfragment = new MyDialogFragment();
dialogfragment.show(getActivity().getFragmentManager(), "");
在 MyDialogFragment 中,它膨胀 XML 并允许用户向 EditTexts 等输入一些值,我希望能够 return 将这些值返回到我调用对话框的任何地方从。
为了这个问题,假设我的对话 class 想要 return 一些私有变量 String mName
和 int mValue
.
在不知道从何处调用对话框(Activity 或片段)的情况下,是否有正确的方法来执行此操作?我如何传回这些值/如何接收它们?
如果你想从fragment
发送数据到activity
。您可以通过以下方式调用 activity
的 public
方法:
((MainActivity)getActivity()).sendData(Object object);
您不能将数据发送到 fragment
。
正如医生所说:
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
你应该做的是:
- 在片段中定义一个
Interface
。
- 在
activity
中实施 Interface
- 将数据传送到
activity
- 然后
activity
将数据传送给其他 fragment
。
顺便说一句,您也可以使用这种做法将数据发送到 activity
(直到第 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
。
正在将数据从 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.
}
创建回调接口并将其传递到您的 dialogfragment
interface DialogValuesCallback {
void callThisFunctionWhenUserClicksOnOkInDialog(String passinmName,int passinmValue);
}
您可以让您的 Activity 或 Fragment 实现此接口。
在你的 MyDialogFragment 中有一个构造函数,它接受接口并将它分配给一个成员变量。
MyDialogFragment(DialogValuesCallback activityOrFragmentWhichImplementsThis){
mInterfaceCallbackObjectRef = activityOrFragmentWhichImplementsThis;
}
@Rohit Arya 这是您首先应该知道的,片段需要声明一个接口,该接口必须由使用该片段的每个 activity 实现,以便您可以将数据从片段传递到 activity(s)... 并且必须像这样在您的 onAttach 方法中将显示当前片段的 activity 投射到此界面中
//@param Listener 是你片段中声明的接口 class
public class MyFragment 扩展片段 {
监听器 mInterface;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mInterface = (Listener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()`
+ " must implement DataListener");
}
}
public interface Listener{
//@param type = data type, cont = variable
public void sendData(type cont, type2 cont2)
;
}
}
确保片段中声明的接口在 baseActivity 中实现,然后使用
传递给接口的数据是什么
mInterface.sendData(value, value1);
你可以在基地 activity 喜欢
public class baseActivity extends Activity implements MyFragment.Listener{
//@param cont & cont2 are data sent from MyFragment
@Override
public void sendData(type cont, type cont2){
//do what ever with your values here
}
}
现在明白了吗?
我这样称呼我的 DialogFragment:
如果我在 Activity:
MyDialogFragment dialogfragment = new MyDialogFragment();
dialogfragment.show(getFragmentManager(), "");
如果我已经在片段中:
MyDialogFragment dialogfragment = new MyDialogFragment();
dialogfragment.show(getActivity().getFragmentManager(), "");
在 MyDialogFragment 中,它膨胀 XML 并允许用户向 EditTexts 等输入一些值,我希望能够 return 将这些值返回到我调用对话框的任何地方从。
为了这个问题,假设我的对话 class 想要 return 一些私有变量 String mName
和 int mValue
.
在不知道从何处调用对话框(Activity 或片段)的情况下,是否有正确的方法来执行此操作?我如何传回这些值/如何接收它们?
如果你想从fragment
发送数据到activity
。您可以通过以下方式调用 activity
的 public
方法:
((MainActivity)getActivity()).sendData(Object object);
您不能将数据发送到 fragment
。
正如医生所说:
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
你应该做的是:
- 在片段中定义一个
Interface
。 - 在
activity
中实施 - 将数据传送到
activity
- 然后
activity
将数据传送给其他fragment
。
Interface
顺便说一句,您也可以使用这种做法将数据发送到 activity
(直到第 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
。
正在将数据从 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.
}
创建回调接口并将其传递到您的 dialogfragment
interface DialogValuesCallback {
void callThisFunctionWhenUserClicksOnOkInDialog(String passinmName,int passinmValue);
}
您可以让您的 Activity 或 Fragment 实现此接口。
在你的 MyDialogFragment 中有一个构造函数,它接受接口并将它分配给一个成员变量。
MyDialogFragment(DialogValuesCallback activityOrFragmentWhichImplementsThis){
mInterfaceCallbackObjectRef = activityOrFragmentWhichImplementsThis;
}
@Rohit Arya 这是您首先应该知道的,片段需要声明一个接口,该接口必须由使用该片段的每个 activity 实现,以便您可以将数据从片段传递到 activity(s)... 并且必须像这样在您的 onAttach 方法中将显示当前片段的 activity 投射到此界面中
//@param Listener 是你片段中声明的接口 class public class MyFragment 扩展片段 { 监听器 mInterface;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mInterface = (Listener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()`
+ " must implement DataListener");
}
}
public interface Listener{
//@param type = data type, cont = variable
public void sendData(type cont, type2 cont2)
;
}
}
确保片段中声明的接口在 baseActivity 中实现,然后使用
传递给接口的数据是什么 mInterface.sendData(value, value1);
你可以在基地 activity 喜欢
public class baseActivity extends Activity implements MyFragment.Listener{
//@param cont & cont2 are data sent from MyFragment
@Override
public void sendData(type cont, type cont2){
//do what ever with your values here
}
}
现在明白了吗?