Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a DialogFragment - using onCreateDialog() or onCreateView()?

I would like to create a home screen with a login button, which when clicked, opens a dialog box to enter a password and then either submit or cancelled.

I have read that there are two methods to do this in order to create a DialogFragment- using onCreateDialog() and onCreateView().

What is the difference between these two options, and which one is used in which scenarios?

like image 470
quartz Avatar asked Jun 28 '16 07:06

quartz


People also ask

How do I start a DialogFragment?

To create a DialogFragment , first create a class that extends DialogFragment , and override onCreateDialog() , as shown in the following example. Similar to how onCreateView() should create a root View in an ordinary fragment, onCreateDialog() should create a Dialog to display as part of the DialogFragment .

What is a DialogFragment?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


1 Answers

The onCreateDialog() is for displaying Basic Dialog. This is the simplest way to display a dialog. While using this method you need to use the builder method of the inbuilt dialog like AlertDialog to build the dialog and listeners for positive and negative button and return a Dialog.

On the other hand OnCreateView() you can return a view to be used as a dialog. You can use your own layout to build a dialog UI. Your own button and listen for them. In simple words your own Custom Dialog.

Also Note: You can even set a view to your built in Dialog like AlerDialog in onCreateDialog() by calling setView() method. The onCreateView() is used to build your dialog from your own layout. Which version should you use is entirely upto you. I hope it helps.

like image 148
Raaja SN Avatar answered Sep 30 '22 12:09

Raaja SN