android.support.v4.app
Class DialogFragment

java.lang.Object
  extended by android.support.v4.app.Fragment
      extended by android.support.v4.app.DialogFragment
All Implemented Interfaces:
android.content.ComponentCallbacks, android.content.DialogInterface.OnCancelListener, android.content.DialogInterface.OnDismissListener, android.view.View.OnCreateContextMenuListener

public class DialogFragment
extends Fragment
implements android.content.DialogInterface.OnCancelListener, android.content.DialogInterface.OnDismissListener

Static library support version of the framework's DialogFragment. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.


Nested Class Summary
 
Nested classes/interfaces inherited from class android.support.v4.app.Fragment
Fragment.InstantiationException, Fragment.SavedState
 
Field Summary
static int STYLE_NO_FRAME
          Style for setStyle(int, int): don't draw any frame at all; the view hierarchy returned by Fragment.onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) is entirely responsible for drawing the dialog.
static int STYLE_NO_INPUT
          Style for setStyle(int, int): like STYLE_NO_FRAME, but also disables all input to the dialog.
static int STYLE_NO_TITLE
          Style for setStyle(int, int): don't include a title area.
static int STYLE_NORMAL
          Style for setStyle(int, int): a basic, normal dialog.
 
Constructor Summary
DialogFragment()
           
 
Method Summary
 void dismiss()
          Dismiss the fragment and its dialog.
 void dismissAllowingStateLoss()
          Version of dismiss() that uses FragmentTransaction.commitAllowingStateLoss().
 android.app.Dialog getDialog()
           
 android.view.LayoutInflater getLayoutInflater(android.os.Bundle savedInstanceState)
           
 boolean getShowsDialog()
          Return the current value of setShowsDialog(boolean).
 int getTheme()
           
 boolean isCancelable()
          Return the current value of setCancelable(boolean).
 void onActivityCreated(android.os.Bundle savedInstanceState)
          Called when the fragment's activity has been created and this fragment's view hierarchy instantiated.
 void onAttach(android.app.Activity activity)
          Called when a fragment is first attached to its activity.
 void onCancel(android.content.DialogInterface dialog)
           
 void onCreate(android.os.Bundle savedInstanceState)
          Called to do initial creation of a fragment.
 android.app.Dialog onCreateDialog(android.os.Bundle savedInstanceState)
          Override to build your own custom Dialog container.
 void onDestroyView()
          Remove dialog.
 void onDetach()
          Called when the fragment is no longer attached to its activity.
 void onDismiss(android.content.DialogInterface dialog)
           
 void onSaveInstanceState(android.os.Bundle outState)
          Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance of its process is restarted.
 void onStart()
          Called when the Fragment is visible to the user.
 void onStop()
          Called when the Fragment is no longer started.
 void setCancelable(boolean cancelable)
          Control whether the shown Dialog is cancelable.
 void setShowsDialog(boolean showsDialog)
          Controls whether this fragment should be shown in a dialog.
 void setStyle(int style, int theme)
          Call to customize the basic appearance and behavior of the fragment's dialog.
 void show(FragmentManager manager, String tag)
          Display the dialog, adding the fragment to the given FragmentManager.
 int show(FragmentTransaction transaction, String tag)
          Display the dialog, adding the fragment using an existing transaction and then committing the transaction.
 
Methods inherited from class android.support.v4.app.Fragment
dump, equals, getActivity, getArguments, getChildFragmentManager, getFragmentManager, getId, getLoaderManager, getParentFragment, getResources, getRetainInstance, getString, getString, getTag, getTargetFragment, getTargetRequestCode, getText, getUserVisibleHint, getView, hashCode, instantiate, instantiate, isAdded, isDetached, isHidden, isInLayout, isRemoving, isResumed, isVisible, onActivityResult, onConfigurationChanged, onContextItemSelected, onCreateAnimator, onCreateContextMenu, onCreateOptionsMenu, onCreateView, onDestroy, onDestroyOptionsMenu, onHiddenChanged, onInflate, onLowMemory, onOptionsItemSelected, onOptionsMenuClosed, onPause, onPrepareOptionsMenu, onResume, onViewCreated, onViewStateRestored, registerForContextMenu, setArguments, setHasOptionsMenu, setInitialSavedState, setMenuVisibility, setRetainInstance, setTargetFragment, setUserVisibleHint, startActivity, startActivityForResult, toString, unregisterForContextMenu
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

STYLE_NORMAL

public static final int STYLE_NORMAL
Style for setStyle(int, int): a basic, normal dialog.

See Also:
Constant Field Values

STYLE_NO_TITLE

public static final int STYLE_NO_TITLE
Style for setStyle(int, int): don't include a title area.

See Also:
Constant Field Values

STYLE_NO_FRAME

public static final int STYLE_NO_FRAME
Style for setStyle(int, int): don't draw any frame at all; the view hierarchy returned by Fragment.onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) is entirely responsible for drawing the dialog.

See Also:
Constant Field Values

STYLE_NO_INPUT

public static final int STYLE_NO_INPUT
Style for setStyle(int, int): like STYLE_NO_FRAME, but also disables all input to the dialog. The user can not touch it, and its window will not receive input focus.

See Also:
Constant Field Values
Constructor Detail

DialogFragment

public DialogFragment()
Method Detail

setStyle

public void setStyle(int style,
                     int theme)
Call to customize the basic appearance and behavior of the fragment's dialog. This can be used for some common dialog behaviors, taking care of selecting flags, theme, and other options for you. The same effect can be achieve by manually setting Dialog and Window attributes yourself. Calling this after the fragment's Dialog is created will have no effect.

Parameters:
style - Selects a standard style: may be STYLE_NORMAL, STYLE_NO_TITLE, STYLE_NO_FRAME, or STYLE_NO_INPUT.
theme - Optional custom theme. If 0, an appropriate theme (based on the style) will be selected for you.

show

public void show(FragmentManager manager,
                 String tag)
Display the dialog, adding the fragment to the given FragmentManager. This is a convenience for explicitly creating a transaction, adding the fragment to it with the given tag, and committing it. This does not add the transaction to the back stack. When the fragment is dismissed, a new transaction will be executed to remove it from the activity.

Parameters:
manager - The FragmentManager this fragment will be added to.
tag - The tag for this fragment, as per FragmentTransaction.add.

show

public int show(FragmentTransaction transaction,
                String tag)
Display the dialog, adding the fragment using an existing transaction and then committing the transaction.

Parameters:
transaction - An existing transaction in which to add the fragment.
tag - The tag for this fragment, as per FragmentTransaction.add.
Returns:
Returns the identifier of the committed transaction, as per FragmentTransaction.commit().

dismiss

public void dismiss()
Dismiss the fragment and its dialog. If the fragment was added to the back stack, all back stack state up to and including this entry will be popped. Otherwise, a new transaction will be committed to remove the fragment.


dismissAllowingStateLoss

public void dismissAllowingStateLoss()
Version of dismiss() that uses FragmentTransaction.commitAllowingStateLoss(). See linked documentation for further details.


getDialog

public android.app.Dialog getDialog()

getTheme

public int getTheme()

setCancelable

public void setCancelable(boolean cancelable)
Control whether the shown Dialog is cancelable. Use this instead of directly calling Dialog.setCancelable(boolean), because DialogFragment needs to change its behavior based on this.

Parameters:
cancelable - If true, the dialog is cancelable. The default is true.

isCancelable

public boolean isCancelable()
Return the current value of setCancelable(boolean).


setShowsDialog

public void setShowsDialog(boolean showsDialog)
Controls whether this fragment should be shown in a dialog. If not set, no Dialog will be created in onActivityCreated(Bundle), and the fragment's view hierarchy will thus not be added to it. This allows you to instead use it as a normal fragment (embedded inside of its activity).

This is normally set for you based on whether the fragment is associated with a container view ID passed to FragmentTransaction.add(int, Fragment). If the fragment was added with a container, setShowsDialog will be initialized to false; otherwise, it will be true.

Parameters:
showsDialog - If true, the fragment will be displayed in a Dialog. If false, no Dialog will be created and the fragment's view hierarchly left undisturbed.

getShowsDialog

public boolean getShowsDialog()
Return the current value of setShowsDialog(boolean).


onAttach

public void onAttach(android.app.Activity activity)
Description copied from class: Fragment
Called when a fragment is first attached to its activity. Fragment.onCreate(Bundle) will be called after this.

Overrides:
onAttach in class Fragment

onDetach

public void onDetach()
Description copied from class: Fragment
Called when the fragment is no longer attached to its activity. This is called after Fragment.onDestroy().

Overrides:
onDetach in class Fragment

onCreate

public void onCreate(android.os.Bundle savedInstanceState)
Description copied from class: Fragment
Called to do initial creation of a fragment. This is called after Fragment.onAttach(Activity) and before Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle).

Note that this can be called while the fragment's activity is still in the process of being created. As such, you can not rely on things like the activity's content view hierarchy being initialized at this point. If you want to do work once the activity itself is created, see Fragment.onActivityCreated(Bundle).

Overrides:
onCreate in class Fragment
Parameters:
savedInstanceState - If the fragment is being re-created from a previous saved state, this is the state.

getLayoutInflater

public android.view.LayoutInflater getLayoutInflater(android.os.Bundle savedInstanceState)
Overrides:
getLayoutInflater in class Fragment

onCreateDialog

public android.app.Dialog onCreateDialog(android.os.Bundle savedInstanceState)
Override to build your own custom Dialog container. This is typically used to show an AlertDialog instead of a generic Dialog; when doing so, Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle) does not need to be implemented since the AlertDialog takes care of its own content.

This method will be called after onCreate(Bundle) and before Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle). The default implementation simply instantiates and returns a Dialog class.

Note: DialogFragment own the Dialog.setOnCancelListener and Dialog.setOnDismissListener callbacks. You must not set them yourself. To find out about these events, override onCancel(DialogInterface) and onDismiss(DialogInterface).

Parameters:
savedInstanceState - The last saved instance state of the Fragment, or null if this is a freshly created Fragment.
Returns:
Return a new Dialog instance to be displayed by the Fragment.

onCancel

public void onCancel(android.content.DialogInterface dialog)
Specified by:
onCancel in interface android.content.DialogInterface.OnCancelListener

onDismiss

public void onDismiss(android.content.DialogInterface dialog)
Specified by:
onDismiss in interface android.content.DialogInterface.OnDismissListener

onActivityCreated

public void onActivityCreated(android.os.Bundle savedInstanceState)
Description copied from class: Fragment
Called when the fragment's activity has been created and this fragment's view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. It is also useful for fragments that use Fragment.setRetainInstance(boolean) to retain their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after Fragment.onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) and before Fragment.onViewStateRestored(Bundle).

Overrides:
onActivityCreated in class Fragment
Parameters:
savedInstanceState - If the fragment is being re-created from a previous saved state, this is the state.

onStart

public void onStart()
Description copied from class: Fragment
Called when the Fragment is visible to the user. This is generally tied to Activity.onStart of the containing Activity's lifecycle.

Overrides:
onStart in class Fragment

onSaveInstanceState

public void onSaveInstanceState(android.os.Bundle outState)
Description copied from class: Fragment
Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance of its process is restarted. If a new instance of the fragment later needs to be created, the data you place in the Bundle here will be available in the Bundle given to Fragment.onCreate(Bundle), Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle), and Fragment.onActivityCreated(Bundle).

This corresponds to Activity.onSaveInstanceState(Bundle) and most of the discussion there applies here as well. Note however: this method may be called at any time before Fragment.onDestroy(). There are many situations where a fragment may be mostly torn down (such as when placed on the back stack with no UI showing), but its state will not be saved until its owning activity actually needs to save its state.

Overrides:
onSaveInstanceState in class Fragment
Parameters:
outState - Bundle in which to place your saved state.

onStop

public void onStop()
Description copied from class: Fragment
Called when the Fragment is no longer started. This is generally tied to Activity.onStop of the containing Activity's lifecycle.

Overrides:
onStop in class Fragment

onDestroyView

public void onDestroyView()
Remove dialog.

Overrides:
onDestroyView in class Fragment


Copyright © 2013 Marek Kedzierski. All Rights Reserved.