Sauron
Sauron

Reputation: 6657

How to call a DialogFragment from an Activity

I wish to simply call a dialog fragment from an activity after clicking a button as:

    eventInviteDecline.setOnClickListener(new View.OnClickListener() {

        EventInviteResponseConfirmDialog newRespond = new EventInviteResponseConfirmDialog();
        FragmentManager fm = getActivity().getFragmentManager();

        public void onClick(View arg0) {
            Bundle args = new Bundle();
            args.putString("response", "decline");
            newRespond.setArguments(args);
            newRespond.show(fm, "ok");

        }

    });

However, within code, it calls an error, noting: Cannot resolve method getActivity().

How can I get this to work?

Upvotes: 1

Views: 4615

Answers (3)

Samir
Samir

Reputation: 6617

I extended my class to FragmentActivity

 FragmentManager fragmentManager = getSupportFragmentManager();
                AlertSMS userPopUp = new AlertSMS();
                userPopUp.show(fragmentManager, "sms");

Upvotes: 1

Usman
Usman

Reputation: 87

You can not call getActivity(), when you are in activity. you can you this. or you can use Activityname.this

Its better to use support fragment manger though when using fragments

getSupportFragmentManager()

Upvotes: 0

mbonnin
mbonnin

Reputation: 7032

If your code is from an Activity, you should just be able to remove the getActivity(). part and call getFragmentManager directly.

 FragmentManager fm = getFragmentManager();

Upvotes: 0

Related Questions