Maggie
Maggie

Reputation: 8081

show dialog without FragmentActivty

This may be rather obvious, but I cannot get it. I am trying to use the suggested way of creating dialogs, by extending DialogFragment. Now, the problem is, I don't know how to invoke it. Documentation states:

DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles");

but my Activity does not extend FragmentActivity (and due to the design of my application, it cannot extend it), so getSupportFragmentManager() cannot be invoked.

Any workaround for this? I would like to skip the deprecated way of creating a dialog.

Upvotes: 0

Views: 180

Answers (3)

imthegiga
imthegiga

Reputation: 1126

well you can directly invoke getSupportFragmentManager(); on FragmentActivity but not on Fragment itself. So to invoke getSupportFragmentManager(); what you can do is first get FragmentActivity and on that call getSupportFragmentManager();. So as you're getting FragmentActivity, you can call this function like:

getActivity().getSupportFragmentManager();

here getActivity() returns FragmentActivity associated with your Fragment.

what you have done is
1. First you have created instance of your custom DialogFragment class.
2. you're showing that dialog.

Now as you've already created an instance of custom DialogFragment why not use that to retrieve support for fragment manager. So what you can do is:

DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(newFragment.getActivity().getSupportFragmentManager(), "missiles");

what this will do is you're getting FragmentActivity associated with newFragment and on that FragmentActivity you're calling getSupportFragmentManager();.

Unfortunately I have not tested it but this might work. Try it and let us know.

Upvotes: -1

An-droid
An-droid

Reputation: 6485

For example you can create a class that extend DialogFragment and invoke it like this:

_dialogFiltre = FragmentDialog.newInstance(R.string.tle_dialog_filter, this);
                _dialogFiltre.setValidDialogListener(this);
                _dialogFiltre.setCancelable(false);
                _dialogFiltre.show(getSupportFragmentManager(), null);

The method new instance :

public static DialogFilter newInstance(int title, Context context) {

    FragmentDialog dialog = new FragmentDialog();
    Bundle args = new Bundle();
    args.putInt("title", title);
    dialog.setArguments(args);
    dialog._context = context;
    return dialog;
}

If you are not in a FragmentActivity i don't see any solution. Why does your application design does not let you use FragmentActivity ?

Upvotes: 0

Budius
Budius

Reputation: 39836

The answer is no.

The FragmentActivity (and it's variants) are the ones that support the FragmentManager and the Fragments.

Common solutions to what you might believe "and due to the design of my application, it cannot extend it"

  • If you're using a ListActivity, just put a list in your layout.
  • If you're using a MapActivity, switch to the new map API.
  • If you're using a TabActivity, that's deprecated, you should use Fragments.

Upvotes: 2

Related Questions