1

I have a DialogPreference, that I'm trying to get a Fragment that is added to it (Google Maps, if anyone cares). The fragment has an id tag in the XML code, and I'm setting the dialog via setDialogLayoutResource(). However, I'm struggling to get a reference to the fragment (So I can set some settings to it, and get data out from it). I need to get a reference to it before the user can start playing with the data.

What I've tried so far is this:

@Override
protected void onBindDialogView(View view) {
    ((Fragment) getDialog()
            .getOwnerActivity()
            .getFragmentManager()
            .findFragmentById(R.id.map));
}

This doesn't work because getDialog returns null at this point in the lifecycle. If there's a better way to reference the activity or FragmentManager, I'd love to see it. Thanks for the help!

4

1 回答 1

2

我设法通过将上下文转换为活动来使其工作。

((Activity) getContext()).getFragmentManager().findFragmentById(R.id.map));
于 2014-02-17T20:03:58.073 回答