如果要从Activity与Fragment进行调用,则从DialogFragment接收数据吗? [英] Receiving data from a DialogFragment if you're calling from an Activity vs a Fragment?

查看:89
本文介绍了如果要从Activity与Fragment进行调用,则从DialogFragment接收数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样称呼我的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 mNameint 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.您可以通过以下方式调用activitypublic方法来做到这一点:

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.

您应该做的是:

  1. 在片段中定义一个Interface.
  2. activity
  3. 中实现Interface
  4. 将数据传递到activity
  5. 然后activity会将数据传递到其他fragment.
  1. Define an Interface in the fragment.
  2. Implement that Interface in the activity
  3. Deliver data to the activity
  4. then activity will deliver data to some other fragment.

顺便说一句,您还可以使用此做法将数据发送到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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆