Friday, 13 February 2015

How to show a Dialog Fragment in Android ?

* Once you have a dialog fragment constructed, you need a fragment transaction to show it.

* Like all other fragments, operations on dialog fragments are conducted through fragment transactions.

* The show() method on a dialog fragment takes a fragment transaction as an input.

* The show() method uses the fragment transaction to add this dialog to the activity and then commits the fragment transaction.

* However, the show() method does not add the transaction to the back stack.

* If you want to do this, you need to add this transaction to the back stack first and then pass it to the show() method.

* The show() method of a dialog fragment has the following signatures:

public int show( FragmentTransaction transaction, String tag)
public int show( FragmentManager manager, String tag)

* The first show() method displays the dialog by adding this fragment to the passed-in transaction with the specified tag. This method then returns the identifier of the commited transaction.

* The second show() method automates getting a transaction from the transaction manager.
This is a shortcut method. However, when you use this second method, you don't have an option to add the transaction to the back stack. If you want that control, you need to use the first  method.
The second method could be used if you wanted to simply display the dialog, and you had no other
reason to work with a fragment transaction at that time.

* A nice thing about a dialog being a fragment is that the underlying fragment manager does the basic state management.

* For example, even if the device rotates when a dialog is being displayed, the dialog is reproduced without you performing any state management.

* The dialog fragment also offers methods to control the frame in which the dialog's view is displayed, such as the title and the appearance of the frame.

No comments:

Post a Comment