将数据发送到对话框

时间:2014-05-27 21:39:53

标签: android

阅读AndroidDevelopers中的DialogFragment文档

http://developer.android.com/reference/android/app/DialogFragment.html

我遇到的BasicDialog的例子似乎很奇怪。要将数据传递给dialogFragment,它使用一个工厂方法,该方法接收数据作为参数并将其存储在一个包中。然后,在onCreate方法上,它解包'来自捆绑包的数据并设置私有字段。为什么他们不使用构造函数来提供这些数据?为什么这种舞蹈是必要的(或者至少是可取的)?

public static class MyDialogFragment extends DialogFragment {
int mNum;

/**
 * Create a new instance of MyDialogFragment, providing "num"
 * as an argument.
 */
static MyDialogFragment newInstance(int num) {
    MyDialogFragment f = new MyDialogFragment();

    // Supply num input as an argument.
    Bundle args = new Bundle();
    args.putInt("num", num);
    f.setArguments(args);

    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNum = getArguments().getInt("num");

    // Pick a style based on the num.
    int style = DialogFragment.STYLE_NORMAL, theme = 0;
    switch ((mNum-1)%6) {
        case 1: style = DialogFragment.STYLE_NO_TITLE; break;
        case 2: style = DialogFragment.STYLE_NO_FRAME; break;
        case 3: style = DialogFragment.STYLE_NO_INPUT; break;
        case 4: style = DialogFragment.STYLE_NORMAL; break;
        case 5: style = DialogFragment.STYLE_NORMAL; break;
        case 6: style = DialogFragment.STYLE_NO_TITLE; break;
        case 7: style = DialogFragment.STYLE_NO_FRAME; break;
        case 8: style = DialogFragment.STYLE_NORMAL; break;
    }
    switch ((mNum-1)%6) {
        case 4: theme = android.R.style.Theme_Holo; break;
        case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
        case 6: theme = android.R.style.Theme_Holo_Light; break;
        case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
        case 8: theme = android.R.style.Theme_Holo_Light; break;
    }
    setStyle(style, theme);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_dialog, container, false);
    View tv = v.findViewById(R.id.text);
    ((TextView)tv).setText("Dialog #" + mNum + ": using style "
            + getNameForNum(mNum));

    // Watch for button clicks.
    Button button = (Button)v.findViewById(R.id.show);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // When button is clicked, call up to owning activity.
            ((FragmentDialog)getActivity()).showDialog();
        }
    });

    return v;
  }
}

3 个答案:

答案 0 :(得分:1)

Fragments可以在任何时候由系统重新创建,如果系统需要这样做,它将调用默认(无参数)构造函数。因此,存储片段配置的唯一方法是通过setArguments,它将在系统需要时存储,序列化和反序列化传递的Bundle。 正如the documentation所说:

  

Fragment的所有子类都必须包含一个公共空构造函数。   框架通常会在需要时重新实例化一个片段类,   特别是在状态恢复期间,需要能够找到这个   构造函数来实例化它。如果没有空构造函数   可用时,在某些情况下会在状态期间发生运行时异常   还原

答案 1 :(得分:0)

在我有限的经验中,我发现当我尝试为片段创建一个构造函数时,它就无法完成。那就是因为只需要在Fragments中使用默认构造函数。 我认为因为DialogFragment实际上是一种片段,这就是为什么它不会让你和你不应该使用构造函数来发送数据。

答案 2 :(得分:0)

没有必要。当您创建DialogFragment时,只需使用函数setArguments将一个参数包传递给它。

DialogFragment dialog = new MyDialogFragment();
Bundle args = new Bundle();
args.putInt(value);
dialog.setArguments(args);
dialog.show(getFragmentManager(), "my_dialog");