问题
I have an activity which extends from ActonBarActivity from the support library.
Now I want to add a dialog in this activity, and following the guide it seems that I have to use the DialogFragment as the dialog container:
These classes define the style and structure for your dialog, but you should use a DialogFragment as a container for your dialog. The DialogFragment class provides all the controls you need to create your dialog and manage its appearance, instead of calling methods on the Dialog object.
Then there is no way to make my activity extends DialogFragment.
And I know the Activity itself own a onCreateDialog(int id) method, but it is deprecated, I wonder what's the alternative?
回答1:
You should create a new class that extends DialogFragment, override its onCreateDialog() method with the code that creates an AlertDialog, and then instantiate that class inside your Activity and call show() on it to make it appear.
来源:https://stackoverflow.com/questions/20159389/create-dialog-in-actionbaractivity