Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package android.support.v4.app;
 
Static library support version of the framework's android.app.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.
 
 public class DialogFragment extends Fragment
         implements DialogInterface.OnCancelListenerDialogInterface.OnDismissListener {

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

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

    
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.
 
     public static final int STYLE_NO_FRAME = 2;

    
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.
 
     public static final int STYLE_NO_INPUT = 3;
 
     private static final String SAVED_DIALOG_STATE_TAG = "android:savedDialogState";
     private static final String SAVED_STYLE = "android:style";
     private static final String SAVED_THEME = "android:theme";
     private static final String SAVED_CANCELABLE = "android:cancelable";
     private static final String SAVED_SHOWS_DIALOG = "android:showsDialog";
     private static final String SAVED_BACK_STACK_ID = "android:backStackId";
 
     int mStyle = ;
     int mTheme = 0;
     boolean mCancelable = true;
     boolean mShowsDialog = true;
     int mBackStackId = -1;
 
     Dialog mDialog;
     boolean mViewDestroyed;
     boolean mDismissed;
     boolean mShownByMe;
 
     public DialogFragment() {
     }

    
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.
    public void setStyle(int styleint theme) {
         = style;
        if ( ==  ||  == ) {
             = android.R.style.Theme_Panel;
        }
        if (theme != 0) {
             = theme;
        }
    }

    
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.
    public void show(FragmentManager managerString tag) {
         = false;
         = true;
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(thistag);
        ft.commit();
    }

    
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().
    public int show(FragmentTransaction transactionString tag) {
         = false;
         = true;
        transaction.add(thistag);
         = false;
         = transaction.commit();
        return ;
    }

    
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.
    public void dismiss() {
        dismissInternal(false);
    }

    
Version of dismiss() that uses FragmentTransaction.commitAllowingStateLoss(). See linked documentation for further details.
    public void dismissAllowingStateLoss() {
        dismissInternal(true);
    }
    void dismissInternal(boolean allowStateLoss) {
        if () {
            return;
        }
         = true;
         = false;
        if ( != null) {
            .dismiss();
             = null;
        }
         = true;
        if ( >= 0) {
                    .);
             = -1;
        } else {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.remove(this);
            if (allowStateLoss) {
                ft.commitAllowingStateLoss();
            } else {
                ft.commit();
            }
        }
    }
    
    public Dialog getDialog() {
        return ;
    }
    public int getTheme() {
        return ;
    }

    
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.
    public void setCancelable(boolean cancelable) {
         = cancelable;
        if ( != null.setCancelable(cancelable);
    }

    
Return the current value of setCancelable(boolean).
    public boolean isCancelable() {
        return ;
    }

    
Controls whether this fragment should be shown in a dialog. If not set, no Dialog will be created in onActivityCreated(android.os.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.
    public void setShowsDialog(boolean showsDialog) {
         = showsDialog;
    }

    
Return the current value of setShowsDialog(boolean).
    public boolean getShowsDialog() {
        return ;
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (!) {
            // If not explicitly shown through our API, take this as an
            // indication that the dialog is no longer dismissed.
             = false;
        }
    }
    @Override
    public void onDetach() {
        super.onDetach();
        if (! && !) {
            // The fragment was not shown by a direct call here, it is not
            // dismissed, and now it is being detached...  well, okay, thou
            // art now dismissed.  Have fun.
             = true;
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         =  == 0;
        if (savedInstanceState != null) {
             = savedInstanceState.getInt();
             = savedInstanceState.getInt(, 0);
             = savedInstanceState.getBoolean(true);
             = savedInstanceState.getBoolean();
             = savedInstanceState.getInt(, -1);
        }
        
    }

    

Hide:
    @Override
    public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
        if (!) {
            return super.getLayoutInflater(savedInstanceState);
        }
         = onCreateDialog(savedInstanceState);
        switch () {
            case :
                .getWindow().addFlags(
                        .. |
                        ..);
                // fall through...
            case :
            case :
                .requestWindowFeature(.);
        }
        if ( != null) {
            return (LayoutInflater.getContext().getSystemService(
                    .);
        }
        return (LayoutInflater.getSystemService(
                .);
    }
    
    
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(android.view.LayoutInflater,android.view.ViewGroup,android.os.Bundle) does not need to be implemented since the AlertDialog takes care of its own content.

This method will be called after onCreate(android.os.Bundle) and before Fragment.onCreateView(android.view.LayoutInflater,android.view.ViewGroup,android.os.Bundle). The default implementation simply instantiates and returns a android.app.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(android.content.DialogInterface) and onDismiss(android.content.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.
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new Dialog(getActivity(), getTheme());
    }
    public void onCancel(DialogInterface dialog) {
    }
    public void onDismiss(DialogInterface dialog) {
        if (!) {
            // Note: we need to use allowStateLoss, because the dialog
            // dispatches this asynchronously so we can receive the call
            // after the activity is paused.  Worst case, when the user comes
            // back to the activity they see the dialog again.
            dismissInternal(true);
        }
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!) {
            return;
        }
        View view = getView();
        if (view != null) {
            if (view.getParent() != null) {
                throw new IllegalStateException("DialogFragment can not be attached to a container view");
            }
            .setContentView(view);
        }
        .setOnCancelListener(this);
        .setOnDismissListener(this);
        if (savedInstanceState != null) {
            Bundle dialogState = savedInstanceState.getBundle();
            if (dialogState != null) {
                .onRestoreInstanceState(dialogState);
            }
        }
    }
    @Override
    public void onStart() {
        super.onStart();
        if ( != null) {
             = false;
            .show();
        }
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if ( != null) {
            Bundle dialogState = .onSaveInstanceState();
            if (dialogState != null) {
                outState.putBundle(dialogState);
            }
        }
        if ( != ) {
            outState.putInt();
        }
        if ( != 0) {
            outState.putInt();
        }
        if (!) {
            outState.putBoolean();
        }
        if (!) {
            outState.putBoolean();
        }
        if ( != -1) {
            outState.putInt();
        }
    }
    @Override
    public void onStop() {
        super.onStop();
        if ( != null) {
            .hide();
        }
    }

    
Remove dialog.
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if ( != null) {
            // Set removed here because this dismissal is just to hide
            // the dialog -- we don't want this to cause the fragment to
            // actually be removed.
             = true;
            .dismiss();
             = null;
        }
    }
New to GrepCode? Check out our FAQ X