Question

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!

Was it helpful?

Solution

I managed to get this to work by casting the Context as an Activity.

((Activity) getContext()).getFragmentManager().findFragmentById(R.id.map));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top